简体   繁体   中英

Finish all activities and start new one

I have one activity which I start with different parameters . I set parameters with intent help. Each parameter have own separate action.

This activity has GridView with content which depends from income parameters.

First start:

Intent intent = new Intent(this, ArticlesListActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

Second start:

startActivity(new Intent(ArticlesListActivity.this, ArticlesListActivity.class));

Third start:

Intent intent = new Intent(this, ArticlesListActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

I need to start this Activity with first parameters, then with other parameters and then again with first parameters. And when I make third launch I need this will be single Activity of Application . Are there a way to do this?

活动具有生命周期,为了在运行同一活动时以不同的参数启动该活动,是要从articleslistactivity的当前实例中再次加载该活动,但要记住再次启动d活动,并且只会再次停止该活动当前实例,您将在堆栈中拥有三个实例。

I think this link you have to helpful for remove all activity in Top of The Stack

Check this link

Clear all activities in a task?

Put this code in Your Class

Intent intent = new Intent(this,destinationactivity.class); intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP | FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Try

Intent intent = new Intent(this, main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM