How to clear stack history?

Consider an application containing actions A, B, C. A starts from the launcher and B starts from A. B has a button. My requirement is to press the button on B in the current activity history. Stack A-> B should clear, and the history stack should contain only C. Is it possible to do this? If so, plz advise me ...

Thanks in advance!

+3
source share
2 answers

Although this is tedious, it can be done by starting with the Activity methods startActivityForResult (), setResult (), finish () and onActivityResult ().

In pseudo code:


A: startActivityForResult(B)
B: startActivityForResult(C)
C: startActivity(D); setResult(CLEAR); finish()
D: ...
B: (onActivityResult) setResult(CLEAR); finish()
A: (onActivityResult) finish()

If you want to change your architecture, a more natural way to do this is to use FLAG_ACTIVITY_CLEAR_TOP for a simple transition from A, B, C to just A.

- A, B C, noHistory, C B A.

+3

"" - "" C, , . , A B , . , , C .

/**
 * Override "Back" key on Android 1.6
 * Don't want user going back to Login or Register forms.
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        goHome();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}    

/**
 * Override "Back" key on Android 2.0 and later
 */
@Override
public void onBackPressed() {
    goHome();
}

private void goHome() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);    
}
0

Source: https://habr.com/ru/post/1792960/


All Articles