How to Use Intent.Flag_Activity_Clear_Top to Clear the Activity Stack

How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack?

@bitestar has the correct solution, but there is one more step:

It was hidden away in the docs, however you must change the launchMode of the Activity to anything other than standard. Otherwise it will be destroyed and recreated instead of being reset to the top.

Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't deletes the activity stack

Use this--

 Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();

Edited--New Answer and would work perfectly..

just taking an example...do it accordingly what your project needs--

I am taking three Activity class A, B and C..and I have applied a close button on the view of class C Activity. If you want then by the Back button you can go to the previous Activity and when you press the close button then you would exit from apps..have a look--

public class AActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent i = new Intent(this, B.class);
startActivityForResult(i, 1);
}

@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (resultCode == 5) {
finish();
}
}
}

Take next class activity--

   public class B extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);

Intent i = new Intent(this, C.class);
startActivityForResult(i, 2);
}

@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (resultCode == 5) {
setResult(5);
finish();
}
}
}

Take last activity--

    public class C extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
}

// close button..set by xml view..you can set it by button listener.
public void close(View v) {
setResult(5);
finish();
}
}

Hopefully, it would solve your problem..cheers!

Activity Do not clear from back stack even From Method Intent.FLAG_ACTIVITY_CLEAR_TOP

Sometimes it doesn't work you should add this line in your code

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK | 
Intent.FLAG_ACTIVITY_CLEAR_TASK);

It will help you and hopefully your issue will resolve.
I've added this line in your code and after adding that line your code will look like this

 Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
progressDialog.dismiss();

Replace your code with the above code.

How To Clear Activity Stack Below An Activity

Oh guys, I figured out that you just have to put the following code with the Intent which starts the stack clearing activity:

Intent i = new Intent(this,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

Thanks for your help though.

Clear ALL activities in back stack

Well, i ended up using this answer

Clear the entire history stack and start a new activity on Android

And that do exactly what Im trying to do



Related Topics



Leave a reply



Submit