jxdwinter
jxdwinter

Reputation: 2369

Android: Back to some Activity

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?

Upvotes: 0

Views: 683

Answers (5)

Renjith
Renjith

Reputation: 5803

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.

Upvotes: 0

dilix
dilix

Reputation: 3893

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.

Upvotes: 0

saran
saran

Reputation: 461

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

Upvotes: 0

sandy
sandy

Reputation: 3341

try this

@Override
 public void onBackPressed() {
     startActivity(new Intent(this, UI.class)
     .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
 return;
}

Upvotes: 0

Egor
Egor

Reputation: 40193

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.

Upvotes: 3

Related Questions