0

enter image description here

Activity1 (not finish) can start Activity2 , Activity2 (not finish) can start Activity3. And Activity2 and 3 can back to previous activity using

super.onBackPressed();
this.finish();

And I want to know how Activity3 back to Activity1(not refresh) directly and release the memory of Activity2?

jxdwinter
  • 2,339
  • 6
  • 34
  • 56
  • http://stackoverflow.com/questions/4342761/how-do-you-use-intent-flag-activity-clear-top-to-clear-the-activity-stack – sandy Dec 20 '12 at 09:59

5 Answers5

3
Intent intent = new Intent(Activity3.this, Activity1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This will make the Activity1 be at the top of the backstack, killing all Activities on top of it. Hope this helps.

Egor
  • 37,239
  • 10
  • 109
  • 127
0

try this

@Override
 public void onBackPressed() {
     startActivity(new Intent(this, UI.class)
     .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
 return;
}
Ram kiran
  • 20,129
  • 14
  • 55
  • 74
sandy
  • 3,346
  • 4
  • 35
  • 45
0

try super.finish(); to close 2nd activity.

saran
  • 471
  • 1
  • 6
  • 20
0

If you want to come back to the existing activity you can use a unique ID and onActivityResult if resultData == ID --> close the second activity (for user it will be seemed like coming back from third activity to the first).

To learn more information about stack of activities, visit google site

Also maybe fragments is suitable for you - and you can simply walk through fragment stack.

dilix
  • 3,510
  • 3
  • 27
  • 53
0

Activity 2

Intent intent = new Intent(Activity2.this, Activity3.class);
startActivityforResult(intent,0);

When coming back from Activity3 override onBackPressed() and set result when needed

setResult(RESULT_CANCELED);

then override onActivityResult() in Activity2

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 0) {
            if(resultCode == RESULT_CANCELED)
                finish();
        }
    }

This will finish second activity, when result is set. Otherwise you can navigate back and forth like any normal activities.

Renjith
  • 5,745
  • 9
  • 28
  • 42