Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Activity and clear activity history

So I have a huge maze of activities in my application. What I need to do, is that when the user logs in into the system, the activity history should be cleared. I cant just use finish() when I start a new activity, because I want the activities to have a history until the user logs in. I have experimentet with the different flags when starting an activity, but I have had no success. Any ideas?

Cheers,

like image 380
pgsandstrom Avatar asked Apr 22 '10 15:04

pgsandstrom


People also ask

How do I delete my Backstack activity?

Use finishAffinity() to clear all backstack with existing one. Suppose, Activities A, B and C are in stack, and finishAffinity(); is called in Activity C, - Activity B will be finished / removing from stack. - Activity A will be finished / removing from stack. - Activity C will finished / removing from stack.

How does Android know which activity to run first?

Unlike programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.

How do I go back to previous activity on Android?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.


1 Answers

I might as well reveal the hax I am currently using to solve my problem. On the "pre-logged in" activities, I have set this in the manifest:

android:noHistory="true"

Then in each activity I have this code:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent intent = new Intent(MyActivity.this, ParentActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
        return true;
    }
    return super.onKeyDown(keyCode, event);

}

The FLAG_ACTIVITY_NO_ANIMATION only works on phones with API level 5 or higher, but what it does is that instead on the "open new activity"-animation, the "going back to previous activity"-animation is played (atleast on the droid and nexus). This prevents the confusing appearence that a new activity is started when the user presses the back-button.

This solution is not perfect. On phones with a API-level lower then 5 the animations becomes incorrect. Also, it is not super neat and requires more code then I prefer. Still, it works...

like image 150
pgsandstrom Avatar answered Nov 15 '22 03:11

pgsandstrom