Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android go back to previous Activity [duplicate]

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.

like image 788
Jonas Avatar asked Mar 15 '13 11:03

Jonas


People also ask

How do I start the same activity again on android?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

How do I know if my android activity is recreated?

You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy . @Override protected void onDestroy() { super. onDestroy(); if (isFinishing()) { // wrap stuff up } else { //It's an orientation change. } }

How do you finish previous activity?

FLAG_ACTIVITY_CLEAR_TASK|Intent. FLAG_ACTIVITY_NEW_TASK . It will totally clears all previous activity(s) and start new activity.


2 Answers

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..

like image 188
Nermeen Avatar answered Oct 11 '22 18:10

Nermeen


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;
}
like image 30
Raghunandan Avatar answered Oct 11 '22 19:10

Raghunandan