how to finish all activities except the first acti

2019-01-26 04:12发布

问题:

I google it but even if i run this code below it didnt finish the other activities.

ButtonClick.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            LoginManager.getInstance().ctrl = false;
            UserManager.getInstance().loginControl();
            OrderManager.getInstance().orderCtrl = false;
            Intent intent = new Intent(OrderComplete.this,
                    MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();
        }
    });
}

回答1:

UPDATE Please refer to the other answers, I cannot delete the answer because it is marked as accepted

As per Our Discussion in Comments

Your Given Code is Fine !

Q1. Why its Not Finishing All the Activities ?

Ans. I think All Activities Are Finished Except Activities who have Thread or AysncTask running in Background or not Finished yet!

Q2. How Can i Finish Them ?

Ans. Make Sure your Thread Should be Finished ! or You can Try Timeout etc !


Additional

Pass String with Your Intent 

for this Add Code

intent.putExtra("finishingallact", "yes");

Inside First Activity (Which is not to be Finished)

Try by Making Object of Remaining Activities to Finish Them !

So Try code :

if(getIntent().getStringExtra("finishingallact")!=null)
{
 if(getIntent().getStringExtra("finishingallact").toLowerCase().equals("yes"))
  {
    yourRemainingAct act1=new yourRemainingAct();
    act1.finish();
    // Try Same For ALl Remaining Activities 
  }
}


回答2:

To clear top activities from stack use below code

Intent intents = new Intent(A.this, B.class);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TOP
            | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intents);
finish();

It will delete all activities from stack either asynctask run or not in the application.

It works fine and also a good approach



回答3:

when your going from one activity to other activity call finish();

do like this

public void onClick(View v) {
  Intent i = new Intent(A.this,B.class);

  i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  startActivity(i);
  finish(); // to end the current activity
 }

call finish() after startactivity(...), so that A activity ll removed from the stack. when you press back button A activity ll be not there in stack.



回答4:

Try this it works fine with me

// clear whole activity stack

    Intent intent = new Intent("clearStackActivity");
    intent.setType("text/plain");
    sendBroadcast(intent);

// start your new activity
Intent intent = new Intent(OrderComplete.this,
                    MainActivity.class);
startActivity(intent);

UPDATE

sorry I forget to give these lines. Put these line in onCreate() method of all Activities or if you have any base activity you can put it there , then no need to put in all activities.

private KillReceiver clearActivityStack;
clearActivityStack = new KillReceiver();
        registerReceiver(clearActivityStack, IntentFilter.create("clearStackActivity", "text/plain"));

UPDATE

So sorry forget one more thing to give put this class in your Base activity

private final class KillReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }


回答5:

just remove every activity from stack except your first activity

youractivity.this.finish();

or you can use the Activity Single top. for that you can use:

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);


回答6:

We can Use :

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  startActivity(i);


回答7:

To remove all the Activities while opening new one, then do the following

  Intent intent = new Intent(getApplicationContext(), NewAcivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);


回答8:

remove android:lanchMode="singleTop" from menifest and use setFlag intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);



回答9:

1.If we haven't the asynctask class or thread in our class then by simply

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

write this code,can easily remove all activities from stack

2.this can also be done by making launcher activity as single task this can be done by changes in manifest file. as

android:launchMode="singleTask" 

3.But if we have asynctask in our app and also use of thread then both above methods fail..

for that we have to finish activity one by one

for eg. We have four activities A,B,C,D and each class has async task

and we have to go from A -> B-> C-> D

and from D We have again come back to *"A" and after pressing back button of device the app should be finish or can say exit*

For that we have to make static object of activities of all classes. Like

 **In class A**
     public static Avtivity mactivity;
     @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_third);
            mactivity=this;
             }

Do this in all classes B,C

And then in D

By pressing a button finish the objects of all classes Like

**mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            B mB=new B();
            mB.mactivity.finish();
                   //////do this for all class which are present in stack of activities
                            finish();
                            ////this will finish the D class and we reach at A class
                    }
                  });**

Thats solve...

I know its not a gud way but also not a bad one...And main thing is its working :)