how to finish all activities except the first acti

2019-01-26 04:01发布

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();
        }
    });
}

9条回答
你好瞎i
2楼-- · 2019-01-26 05:00

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 
  }
}
查看更多
Melony?
3楼-- · 2019-01-26 05:03

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();
        }
    }
查看更多
叛逆
4楼-- · 2019-01-26 05:07

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);
查看更多
登录 后发表回答