简体   繁体   English

停止Android应用程序的所有活动和片段

[英]Stop all activities and fragments of Android application

To disconnect from the application i'm developing, i stop all activities and start the LoginActivity. 要断开与我正在开发的应用程序的连接,请停止所有活动并启动LoginActivity。 To do so i'm using the code below. 为此,我使用下面的代码。 the LoginActivity is started but when i click the back button of my phone, i'm connected again without giving the login and password. LoginActivity已启动,但是当我单击手机的后退按钮时,我再次连接,但未提供登录名和密码。

NB: I have also fragments in my app! 注意:我的应用程式中也有片段!

Can you help me pllz. 你能帮我吗。

// After logout redirect user to Loing Activity
        Intent i = new Intent(_context, LoginActivity.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Staring Login Activity
        _context.startActivity(i);

The call i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) sets the flag Intent.FLAG_ACTIVITY_CLEAR_TOP in the Intent flags. 调用i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)在Intent标志中设置标志Intent.FLAG_ACTIVITY_CLEAR_TOP Then you do this: 然后您执行以下操作:

    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

This sets the content of the Intent flags to Intent.FLAG_ACTIVITY_NEW_TASK , thereby clearing the flag Intent.FLAG_ACTIVITY_CLEAR_TOP that you had previously set! Intent.FLAG_ACTIVITY_NEW_TASK Intent标志的内容设置为Intent.FLAG_ACTIVITY_NEW_TASK ,从而清除您先前设置的标志Intent.FLAG_ACTIVITY_CLEAR_TOP

You don't need to use Intent.FLAG_ACTIVITY_NEW_TASK , as this won't actually start a new task (because your activities all have the same taskAffinity ). 您不需要使用Intent.FLAG_ACTIVITY_NEW_TASK ,因为这实际上不会启动新任务(因为所有活动都具有相同的taskAffinity )。

Setting Intent.FLAG_ACTIVITY_CLEAR_TOP should be enough to do what you want. 设置Intent.FLAG_ACTIVITY_CLEAR_TOP应该足以完成您想要的操作。 However, you will need to make sure that your LoginActivity is still active in the task (ie: you shouldn't have called finish() on it). 但是,您需要确保您的LoginActivity在任务中仍然处于活动状态(即:您不应该在其上调用finish() )。 If your LoginActivity isn't present in the activity stack of your task (because you have already called finish() on it), then Android will just create a new instance of LoginActivity and put it on top of the stack. 如果您的任务的活动堆栈中不存在您的LoginActivity (因为您已经在其上调用了finish() ),则Android只会创建一个新的LoginActivity实例并将其放在堆栈顶部。

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

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