简体   繁体   English

清除android中的所有活动

[英]Clearing all activities in android

I have 4 Activities 1.Home,2.B,3.C and 4.D. 我有4项活动1.Home,2.B,3.C和4.D. Whenever I start Home from Activity DI want to finish all other activities. 每当我从活动开始回家DI想要完成所有其他活动。 I Used this code, but when I press back button from Home it brings me to the previous activity. 我使用了这段代码,但当我从Home按回按钮时,它会将我带到上一个活动。 What I did wrong here.? 我在这里做错了什么。

    Intent intent = new Intent(getApplicationContext(), Home.class);        
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent)

You can try this, 你可以试试这个,

Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Note: As described in FLAG_ACTIVITY_CLEAR_TOP documentation 注意:如FLAG_ACTIVITY_CLEAR_TOP文档中所述

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. 此启动模式也可以与FLAG_ACTIVITY_NEW_TASK结合使用:如果用于启动任务的根活动,它将把该任务的任何当前运行的实例带到前台,然后将其清除到其根状态。 This is especially useful, for example, when launching an activity from the notification manager. 这在从通知管理器启动活动时尤其有用。

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

This will work only for activities which are still in the activity stack. 这仅适用于仍在活动堆栈中的活动。 I believe you are finishing the Home Activity when going to B. So that CLEARTOP won't work. 我相信你在去B的时候正在完成家庭活动。所以CLEARTOP将不起作用。 Now try something like this. 现在尝试这样的事情。

You need to set an Extra with intent Of "D" to Home. 您需要在Home中设置额外的“D”意图。 Then you have to check the Intent extra in Home, call finish() if the extra matching 然后你必须在Home中检查Intent extra,如果额外匹配则调用finish()

    Intent intent = new Intent(contxt, Home.class);
    intent.putExtra("urString",defaultvalue);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    // Checking the intent extra at "HOME"
    if(getIntent().hasExtra("urString")){
      // manage your own way            
      finish();
    }

在manifest.xml文件中为所有活动设置android:nohistroy =“true”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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