0

in my app i am using menu back button to go back to previous activity. one of my activity is getting intent from its previous activity and after doing http call it is sending intent to next activity so flow is kind of A1--intent--> A2 -- intent--> A3 . the problem is when i come back to activity A2 from A3 it crashes. the reason would be empty intent i believe. so i added below code in Activities A3 and A2

activity A3

public boolean onOptionsItemSelected(MenuItem menuItem) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (menuItem.getItemId()) {
    case android.R.id.home:
      Intent homeIntent = new Intent(context, AllClasses.class);
      homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      homeIntent.putExtras(b); // bundle b has the required data for A2
      startActivity(homeIntent);
    }
    return super.onOptionsItemSelected(menuItem);
}

activity A2

     protected void onReStart(){

    // retreiving data from intent
            Bundle b = new Bundle();
            b = getIntent().getExtras();
            String imessage_string = b.getString("imessage");
            Log.d("onrestart",imessage_string);
}

Problem :- now the problem is that now if i press back button on menu bar of A3 first i see the message "unfortulately project has stopped " and then after pressing "OK" A2 starts like i have moved from A1 to A2 and makes call to asyncTask. if i comment "homeIntent.putExtras(b);" in A3 then i recevie the error message twice and jump directly to A1. i am sure on returning to A2 my onRestart() method is not being called as i havn't made any call to asyncTask in that. Does anyone gets any idea what i should do here ? Thanks for you help in advance.... :)

Pawan Rawat
  • 495
  • 1
  • 7
  • 25
  • 1
    you want to use onResume(), which you must override, not onReStart(). Also, if the actionbar handles going to the home activity you shouldn't also explicitly start it in onOptionsItemsSelected() – Martin Dec 01 '14 at 19:19
  • okay...my motto is to go to just previous screen and if i don't use onOptionsltemsSelected() then how do i pass the id back to A2 which is required to call the asyncTask method? – Pawan Rawat Dec 01 '14 at 19:27
  • as per the activity life cycle depicted here http://developer.android.com/training/basics/activity-lifecycle/pausing.html i should use onReStart() as when i call another activity my previous activity is stopped(hidden) ... don't you agree ? – Pawan Rawat Dec 01 '14 at 19:45
  • there just isn't a onReStart(), so I hope that works for you. Can't see how, though – Martin Dec 02 '14 at 17:49
  • actually it isn't working... i added both onrestart and onresume methods in my A2 activity and i hoped they print the log before the app crashed but the app crashes even before anyone of them is called ...can you help here ? – Pawan Rawat Dec 02 '14 at 18:01
  • post your logcat. it will show Where/why it is crashing. If that doesn't help, post your entire activity code, too – Martin Dec 03 '14 at 19:29

1 Answers1

0

If you want to pass something from A3 to A2 (A stand for activity) consider using onActivityResult in A2

Look at the answer, hope it helps

Community
  • 1
  • 1
imran3
  • 424
  • 1
  • 4
  • 10
  • thanks @imran... i wasn't using A3 to calculate smthing for A2 . the case was simple like going to next page and returning back. However the problem has been resolved. i had to declare the activity A2 as "launchmode=singleTop" so that my system keeps its instance alive in backstack and then from A3 i had to call navutils.navigateupfromsametask(this)" to bring the instance of A2 on top from backstack...nothing i needed to pass to A2 from A3. – Pawan Rawat Dec 04 '14 at 18:17