Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't deletes

2019-01-25 05:16发布

I am developing the application in which i want to close whole application on button click. I know in android we should not think about to close the application because android does that automatically from this Is quitting an application frowned upon?. but yet i want to close my application.

So what i am doing to close application is i am using Intent.FLAG_ACTIVITY_CLEAR_TOP flag to delete the activity stack.

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

And in onCreate of FinishActivity.class i am calling this.finish() but application is not get closed and previous activity gets reopened.

FinishActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.finish();
}

Update :

Here is the scenario

MainActivity->Activity2->Activity3->FinishActivity

Here Activity2 is gets opened after finishing the activity.

How do i achieve this? Any idea and suggestion will be appreciated.

Thanks & Regards

10条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-25 05:56

Give this a try. This should clear your activity stack.

Intent i = new Intent(this,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
查看更多
可以哭但决不认输i
3楼-- · 2019-01-25 06:00

Try this:

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
查看更多
虎瘦雄心在
4楼-- · 2019-01-25 06:04

From documentation for Intent.FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

So to get it work your FinishActivity must be first one in your Activity stack. In any other cases this solution wouldn't give you anything.

You must perform several steps to perform this:

1) Make FinishActivity as your launcher activity.

2) Do not provide any view for it and start first Activity of your application directly from onCreate callback :

3) Redefine onRestart callback:

Code sample:

private boolean isNeedToContinue = true;

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
}

@Override
protected void onResume() {
    super.onResume();
    if (isNeedToContinue) {
                    startActivity(new Intent(this,FirstVisibleActivity.class));
                    isNeedToContinue = false;
    } else {
        finish();
    }
}

@Override
protected void onRestart() {
    super.onRestart();
    finish();
    isNeedToContinue = false;
}

I guess this is all you need. Good luck!

查看更多
三岁会撩人
5楼-- · 2019-01-25 06:05

This worked for me:

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
查看更多
你好瞎i
6楼-- · 2019-01-25 06:06

Also you can use finishAffinity() function in last activity like:

finishAffinity()
startHomeActivity()

Hope it'll be useful.

查看更多
孤傲高冷的网名
7楼-- · 2019-01-25 06:08

setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP);

apart from that set launchMode to "singleTop" in FinishActivity definition in xml, overwrite onNewIntent method , you can pass some additional information as part of intent , instated of finishing your activity in onCreate finish it in onNewIntent method of activity based on some signal from calling activity or simply finish it based on your need . It maybe possible your other activities have different lauchmodes that's why they are not finishing .

查看更多
登录 后发表回答