0

I have list of activities stack as a form but once the form finished I wanted to clear all the stack apart from the first activity but display a new activity so when clicking back on new activity will go back to the first activity.

E.g. A -> B -> C -> D then become A -> E so the E will be visible to the user after submitting the form on D..

Do I launch in this way?

Intent message = new Intent();
message.setFlag(ACTIVITY_FLAG_CLEAR_TOP)
message.addClass(getContext(), E.class)
startActivity(E.class);
LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • You can use Finish() method or add flags to the intents. http://stackoverflow.com/a/24524014/5577385 – CCL Nov 18 '15 at 22:32
  • I think this will not work. CLEAR_TOP means that if you start A,B orC, the activities above it will be cleared. if you use finish when launch C and D, you can achieve A->E, but D can not back to C and C can not back to B. while if you do want A->B->C->D then become A->E, one possible way is use StartActivityForResult and handle the onActivityForResult in A,B,C,D and in A, you start E. – RyanShao Nov 19 '15 at 04:39
  • Do you mean finish every activity on each of the onActivityForResult – LittleFunny Nov 19 '15 at 06:01

2 Answers2

0

I think the correct one is FLAG_ACTIVITY_CLEAR_TOP

Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
cw fei
  • 1,534
  • 1
  • 15
  • 33
0

I'll show you the way I do it when I want to logout from an app.

This can work for you to finish activities B -> C -> D and start activity A

First I create a logout method in a class with session details

public void logoutUser(){

    clearSharedPreferences();

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("com.test.your.packgage.ACTION_LOGOUT");
    _context.sendBroadcast(broadcastIntent);

    // We send a broadcast to all registered activities
    // this way all activities will run finish ()
    // And they will be automatically closed
    LocalBroadcastManager.getInstance(_context).sendBroadcast(broadcastIntent);

}

public void clearSharedPreferences() {
    // Clearing all data from Shared Preferences
    editor.clear();
    editor.commit();
}

Then, in every activity you have to do this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.test.your.packgage.ACTION_LOGOUT");
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("onReceive","Logout in progress");

            finish();
        }
    }, intentFilter);
}
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97