android - checking if mainActivity is called from another activity -
android - checking if mainActivity is called from another activity -
in application go mainactivity
trackingactivity
, later mainactivity
. in lastly step utilize putextra's. code going mainactivity
:
public void gotohomewithdata(){ intent intent = new intent(this, mainactivity.class); intent.putextra("avgspeed", avgspeed); intent.putextra("totaldistance", totaldistance); intent.putextra("altitude", altitude); intent.putextra("totaltime", totaltime); intent.addflags(intent.flag_activity_single_top); startactivity(intent); }
in onresume()
method in mainactivity
i'd retreive info like:
string totaltime = getintent().getextras().getstring("totaltime"); double totaldistance = getintent().getextras().getdouble("totaldistance"); double avgspeed = getintent().getextras().getdouble("avgspeed"); double height = getintent().getextras().getdouble("altitude");
but because onresume()
called when starting application, app crashes on getintent().getextras().getstring("totaltime");
part since there no intent called mainactivity
.
what i'm looking check around getintent
s see if mainactivity
called trackingactivity
or not.
thank in advance.
you should startactivityforresult;
in activity 1
protected void launchsecondactivity() { // todo auto-generated method stub intent = new intent(this,secondactivity.class); startactivityforresult(i, 0); }
in sec activity instead of launching new instance of first activity utilize setresult
public void gotohomewithdata(){ intent intent = new intent(); intent.putextra("avgspeed", avgspeed); intent.putextra("totaldistance", totaldistance); intent.putextra("altitude", altitude); intent.putextra("totaltime", totaltime); intent.addflags(intent.flag_activity_single_top); setresult(result_ok,intent); finish(); }
then in first activity can hear result so:
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok && data!=null) { if (data.getextras().containskey("sometag") { theextrayoucareabout = data.getstringextra("sometag"); } }
note pseudo code should set on right path
android android-intent android-activity bundle
Comments
Post a Comment