Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - clear history when navigating between Activities

I have 3 Activities that my user continuously is looping through. When user is back to the main screen I need to terminate previous history so user cannot hit back button and end up on screen #2, what would be a good way to do something like that? BTW - I'm using 1.6 (API level 4)

To reiterate - say I don't know or predict the path which leads me to the original view. But once I load it I want to clear history that led user to that view. In 2.0 it's possible with overwriting Activity#onBackPressed but I need something like that in 1.6

like image 209
Bostone Avatar asked Nov 17 '09 18:11

Bostone


1 Answers

Ok, I assume you have 3 activities, A, B and C. A is the main screen and user will loop through these 3 pages. But when user enter A, the onBackPresed event should be performed as exit. Do i make it clear?

In such situation, when you try to start A from B or C, you can add Intent.FLAG_ACTIVITY_CLEAR_TOP to the Intent, then the history stack will be cleared and there will be only A in your stack.

If you want to intercept the back key event, you do not need to override onBackPressed(). We always use onKeyDown before this method is available.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

    }
    return super.onKeyDown(keyCode, event);
}
like image 194
faylon Avatar answered Nov 11 '22 10:11

faylon