6

I have two activities, MainActivity and NextActivity. From MainActivity I can go to NextActivity and then use Intent to go back to MainActivity. But then OnCreate and other stuff will be called and it will be like everything is initialized again. I want to go back to the state just like it was before i entered the NextActivity.

I realized that if I use the physical back button on my phone exactly this is achieved.

So how to go back to an activity just like the back button?

PS. I tested finish() but didn't help.

Nermeen
  • 15,883
  • 5
  • 59
  • 72
Jonas
  • 1,112
  • 5
  • 17
  • 28
  • 3
    did you really test finish? how? that's the way – lelloman Mar 15 '13 at 11:08
  • 2
    finish() is the way to go. What does it mean that it didn't help? Maybe you're running out of memory and Android has to rebuild your Activity? – Zielony Mar 15 '13 at 11:10
  • I don't think there's any guarantee that your previous activity will still be available. You're better off saving the state using preferences or some sort of storage in case the activity has been garbage collected. – Davos555 Mar 15 '13 at 11:12
  • If pressing back button achieves what you want, do not create a new intent for going back. Remove finish() from your first activity, and in your second activity go back with calling finish() – akaya Mar 15 '13 at 11:15
  • You're all right, i had a small button which was pressed instead of the big button which was calling finish() (So the small button which I was pressing was calling intent, my bad, sorry! :) ) – Jonas Mar 15 '13 at 12:38

2 Answers2

25

you can just call onBackPressed() instead of use Intent to go back to MainActivity..

For example:

public void onClick() {
    onBackPressed();
}

Note: finish() should do exactly what you want..

Nermeen
  • 15,883
  • 5
  • 59
  • 72
4

Assume you have two Activities A and B. You navigate from A to B. A goes to background .

B is put on back stack and B takes focus. When you click back button activity B pops out of back stack. Activity A is resumed.

Note: Multiple tasks can be held in the background at once. However, if the user is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost. See the following section about Activity state.

http://developer.android.com/training/basics/activity-lifecycle/starting.html. Activity once destroyed has to be recreated. Activity is destoyed and recreated when the screen orientation changes.

http://developer.android.com/training/basics/activity-lifecycle/starting.html.

http://developer.android.com/guide/components/tasks-and-back-stack.html. You should have a look at how back stack works.

In your case finish should work for you (by pressing the back button).

Note :The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method.

Suppose you have a third activity C and you want to go to Activity A .

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    onBackPressed();

}

return super.onKeyDown(keyCode, event);
}

 public void onBackPressed() {
Intent myIntent = new Intent(ActivityC.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// clear back stack
startActivity(myIntent);
finish();
return;
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256