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,
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With