Skip to content Skip to sidebar Skip to footer

Android Return To Same Activity Finish Old One

Hello I have a list activity getting data from internet and a search activity. When user press search button search activity starts and gets filters from user. Then when user pres

Solution 1:

I think what you're looking for is the Intent flags, and I think the one interesting for you is: FLAG_ACTIVITY_CLEAR_TOP (http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP)

final Intent i = newIntent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

Let me know if that worked.

Solution 2:

when you call the new activity finish the current one like so:

finish();

Also read this: Android LifeCycle

Solution 3:

You can use android:launchMode="singleTop" for your "A" activity. Be aware that in this case onNewIntent method will be called inside your "A" activity and your activity should react on it appropriately to your needs.

Solution 4:

You can useandroid:launchMode="singleTask"

The conclusion -

A --> B (B is finished)
A (this is activity stack, lets make another search)
A --> B (b is finished)
A (ovvv yes)

You will never have other instance of your activity A Or B.

singleTask

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

Reference: http://developer.android.com/guide/topics/manifest/activity-element.html

Solution 5:

Not sure I understand what you want but instead of finish your activity, you can chose to do something with the "back button"

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
            && !event.isCanceled()) {
        // TODO what you want 

        Intent intent = new Intent(ChoixPiecesJustificativesActivity.this,
                MainActivity.class);
        startActivity(intent);
        returntrue;
    }
    return super.onKeyUp(keyCode, event);
}

Post a Comment for "Android Return To Same Activity Finish Old One"