android - my second activity is not returning result for first one -
i trying make app in mainactivity
ask result main2activity
code mainactivity
is:
package hello.option_menu; import android.app.activity; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import com.ye.collor.stackapplication.r; public class mainactivity extends appcompatactivity { button bt; textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button bt=(button)findviewbyid(r.id.bt); textview tv=(textview)findviewbyid(r.id.tv) ; bt.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { intent i=new intent(mainactivity.this,main2activity.class); startactivityforresult(i,1); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(requestcode==1) { string message=data.getstringextra("message"); tv.settext(message); } } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.jai, menu); return true; } }
code of main2activity
is:
package hello.option_menu; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.view; import android.widget.button; import android.widget.edittext; import com.ye.collor.stackapplication.r; public class main2activity extends appcompatactivity { button bt; edittext et; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2); bt=(button)findviewbyid(r.id.button); et=(edittext)findviewbyid(r.id.et); bt.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { string message=et.gettext().tostring(); intent intent=new intent(); intent.putextra("message",message); setresult(1,intent); finish(); } }); } public boolean oncreateoptionsmenu(menu menu) { menu.add("hello"); menuinflater inflater=getmenuinflater(); inflater.inflate(r.menu.jai,menu); return true; } }
so view of app is:my mainactivity
so when click send button after typing text in main2activity
set text written in mainactivity
didn't that.it still remain "hello world",so text not change .i unable trouble shoot problem.
in mainactivity2
change
setresult(1,intent);
to
setresult(result_ok,intent);
what trying set hardcoded value of result_type
1 int value result_first_user
whereas intended set result_ok
also value of 1 assuming requestcode in m=mainactivity2 not request_code
result_code
. requestcode remains in mainactivity , can used distinguish between different activities open.
also can change onactivityresult
follows keep more safer
protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(requestcode==1 && resultcode==result_ok) { string message=data.getstringextra("message"); tv.settext(message); } }
update 1 mentioned crashes after this, because textview not initialized in mainactivity.
so in mainactivity change
textview tv=(textview)findviewbyid(r.id.tv) ;
to
tv=(textview)findviewbyid(r.id.tv) ;
so textview declared globally assigned value when return in onactivityresult
Comments
Post a Comment