简体   繁体   English

完成所有之前的活动

[英]Finish all previous activities

My application has the following flow screens:我的应用程序具有以下流程屏幕:

Home->screen 1->screen 2->screen 3->screen 4->screen 5

Now I have a common log out button in each screens现在我在每个屏幕上都有一个通用的log out按钮

( Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5 ) Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5

I want that when user clicks on the log out button(from any screen), all the screens will be finished and a new screen Log in will open.我希望当用户单击注销按钮(从任何屏幕)时,所有屏幕都将完成并打开一个新屏幕Log in

I have tried nearly all FLAG_ACTIVITY to achieve this.我已经尝试了几乎所有的FLAG_ACTIVITY来实现这一点。 I also go through some answers in stackoverflow, but not being able to solve the problem.我也在go通过stackoverflow中的一些答案,但无法解决问题。 My application is on Android 1.6 so not being able to use FLAG_ACTIVITY_CLEAR_TASK我的应用程序在 Android 1.6 上,所以无法使用FLAG_ACTIVITY_CLEAR_TASK

Is there any way to solve the issue?有什么办法可以解决这个问题吗?

Use:利用:

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

This will clear all the activities on top of home.这将清除家庭顶部的所有活动。

Assuming you are finishing the login screen when the user logs in and home is created and afterwards all the screens from 1 to 5 on top of that one.假设您在用户登录并创建主页时完成登录屏幕,然后在该屏幕之上从 1 到 5 的所有屏幕。 The code i posted will return you to home screen finishing all the other activites.我发布的代码将使您返回主屏幕,完成所有其他活动。 You can add an extra in the intent and read that in the home screen activity and finish it also (maybe launch login screen again from there or something).您可以在意图中添加额外内容并在主屏幕活动中阅读并完成它(也许从那里再次启动登录屏幕或其他东西)。

I am not sure but you can also try going to login with this flag.我不确定,但您也可以尝试使用此标志登录。 I don't know how the activities will be ordered in that case.我不知道在这种情况下如何安排活动。 So don't know if it will clear the ones below the screen you are on including the one you are currently on but its definitely the way to go.所以不知道它是否会清除您正在使用的屏幕下方的内容,包括您当前正在使用的内容,但它绝对是通往 go 的方式。

Hope this helps.希望这可以帮助。

You may try Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK .您可以尝试Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK It will totally clears all previous activity(s) and start new activity.它将完全清除所有以前的活动并开始新的活动。

Before launching your new Activity, simply add the following code:在启动新 Activity 之前,只需添加以下代码:

finishAffinity();

Or if you want it to work in previous versions of Android:或者,如果您希望它在以前版本的 Android 中工作:

ActivityCompat.finishAffinity(this);

When the user wishes to exit all open activities , they should press a button which loads the first Activity that runs when your application starts, clear all the other activities, then have the last remaining activity finish.当用户希望退出所有打开的活动时,他们应该按下一个按钮,加载应用程序启动时运行的第一个活动,清除所有其他活动,然后完成最后一个剩余活动。 Have the following code run when the user presses the exit button.当用户按下退出按钮时,运行以下代码。 In my case, LoginActivity is the first activity in my program to run.就我而言, LoginActivity是我的程序中要运行的第一个活动。

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

The above code clears all the activities except for LoginActivity .上面的代码清除了除LoginActivity之外的所有活动。 Then put the following code inside the LoginActivity 's onCreate(...) , to listen for when LoginActivity is recreated and the 'EXIT' signal was passed:然后将以下代码放入LoginActivityonCreate(...)中,以侦听何时重新创建LoginActivity并传递了“EXIT”信号:

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

Why is making an exit button in Android so hard?为什么在 Android 中制作退出按钮这么难?

Android tries hard to discourage you from having an "exit" button in your application, because they want the user to never care about whether or not the programs they use are running in the background or not. Android 极力劝阻您不要在应用程序中使用“退出”按钮,因为他们希望用户永远不会关心他们使用的程序是否在后台运行。

The Android OS developers want your program to be able to survive an unexpected shutdown and power off of the phone, and when the user restarts the program, they pick up right where they left off. Android 操作系统开发人员希望您的程序能够在手机意外关机和断电的情况下幸存下来,并且当用户重新启动程序时,他们会从上次中断的地方继续。 So the user can receive a phone call while they use your application, and open maps which requires your application to be freed for more resources.因此,用户可以在使用您的应用程序时接听电话,并打开需要释放您的应用程序以获取更多资源的地图。

When the user resumes your application, they pick up right where they left off with no interruption.当用户恢复您的应用程序时,他们会从上次中断的地方继续,不会中断。 This exit button is usurping power from the activity manager, potentially causing problems with the automatically managed android program life cycle.这个退出按钮正在篡夺活动管理器的权力,可能会导致自动管理的 android 程序生命周期出现问题。

Intent intent = new Intent(this, classObject);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

This Will work for all Android versions.这适用于所有 Android 版本。 Where IntentCompat the class added in Android Support library.其中IntentCompat将 class 添加到 Android 支持库中。

Use the following for activity使用以下活动

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

remove CLEAR_TASK flag for fragment use.删除 CLEAR_TASK 标志以供片段使用。

I hope this may use for some people.我希望这可能对某些人有用。

I guess I am late but there is simple and short answer.我想我迟到了,但有一个简单而简短的答案。 There is a finishAffinity() method in Activity that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher. Activity 中有一个finishAffinity() 方法可以完成当前活动和所有父活动,但它只在Android 4.1 或更高版本中有效。

For API 16+, use对于 API 16+,使用

finishAffinity();

For below 16, use对于 16 岁以下,使用

ActivityCompat.finishAffinity(YourActivity.this);

Hope it helps!希望能帮助到你!

On a side note, good to know顺便说一句,很高兴知道
This answer works ( https://stackoverflow.com/a/13468685/7034327 )这个答案有效( https://stackoverflow.com/a/13468685/7034327

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
this.finish();

whereas this doesn't work而这不起作用

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

.setFlags() replaces any previous flags and doesn't append any new flags while .addFlags() does. .setFlags()替换任何以前的标志,并且不会 append 任何新标志,而.addFlags()会。

So this will also work所以这也可以

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

From developer.android.com :来自开发人员.android.com

 public void finishAffinity ()

Added in API level 16在 API 级别 16 中添加

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.在当前任务中完成此活动以及紧接其下的所有具有相同亲和力的活动。 This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task.这通常在应用程序可以启动到另一个任务时使用(例如从它理解的内容类型的 ACTION_VIEW)并且用户已经使用向上导航切换出当前任务并进入它自己的任务。 In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.在这种情况下,如果用户向下导航到第二个应用程序的任何其他活动,则所有这些活动都应作为任务切换的一部分从原始任务中删除。

Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.请注意,此完成不允许您将结果传递给先前的活动,如果您尝试这样做,则会引发异常。

If your application has minimum sdk version 16 then you can use finishAffinity()如果您的应用程序具有最低 sdk 版本 16,那么您可以使用finishAffinity()

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.在当前任务中完成此活动以及紧接其下的所有具有相同亲和力的活动。

This is work for me In Top Payment screen remove all back-stack activits,这对我有用 在 Top Payment 屏幕中删除所有后台活动,

@Override
public void onBackPressed() {
         finishAffinity();
        startActivity(new Intent(PaymentDoneActivity.this,Home.class));
    } 

http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29 http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29

For logout button on last screen of app, use this code on logout button listener to finish all open previous activities, and your problem is solved.对于应用程序最后一个屏幕上的注销按钮,在注销按钮侦听器上使用此代码来完成所有打开的先前活动,您的问题就解决了。

{
Intent intent = new Intent(this, loginScreen.class);
ntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}

In my case I use finishAffinity() function in last activity like:就我而言,我在最后一个活动中使用finishAffinity() function,例如:

finishAffinity()
startHomeActivity()

Hope it'll be useful.希望它会有用。

A solution I implemented for this (I think I found it on Stack Overflow somewhere, but I don't remember, so thanks to whoever did that in the first place):我为此实施了一个解决方案(我想我在某个地方的 Stack Overflow 上找到了它,但我不记得了,所以感谢首先这样做的人):

From any of your activities do this:从您的任何活动中执行以下操作:

// Clear your session, remove preferences, etc.
Intent intent  = new Intent(getBaseContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Then in your LoginActivity, overwrite onKeyDown:然后在您的 LoginActivity 中,覆盖 onKeyDown:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

i have same problem you can use IntentCompat , like this:我有同样的问题,你可以使用IntentCompat ,像这样:

import android.support.v4.content.IntentCompat;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);

this code work for me.这段代码对我有用。

Android api 17 Android api 17

    Intent i1=new Intent(getApplicationContext(),StartUp_Page.class);
i1.setAction(Intent.ACTION_MAIN);
i1.addCategory(Intent.CATEGORY_HOME);
i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i1);
finish();

instead of using finish() just use finishAffinity();而不是使用finish() ,只需使用finishAffinity();

for API >= 15 to API 23 simple solution.对于 API >= 15 到 API 23 简单解决方案。

 Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
 nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
 startActivity(nextScreen);
 ActivityCompat.finishAffinity(currentActivity.this);

Log in->Home->screen 1->screen 2->screen 3->screen 4->screen 5登录->首页->屏幕1->屏幕2->屏幕3->屏幕4->屏幕5

on screen 4 (or any other) ->在屏幕 4(或任何其他)->

StartActivity(Log in)
with
FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_CLEAR_TOP

If you are using startActivityForResult() in your previous activities, just override OnActivityResult() and call the finish();如果您在之前的活动中使用startActivityForResult() ,只需覆盖OnActivityResult()并调用finish(); method inside it in all activities.. This will do the job...在所有活动中使用它里面的方法..这将完成这项工作......

I guess I am late but there is simple and short answer.我想我迟到了,但有一个简单而简短的答案。 There is a finishAffinity() method in Activity that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher. Activity 中有一个finishAffinity() 方法可以完成当前活动和所有父活动,但它只在Android 4.1 或更高版本中有效。

For API 16+, use对于 API 16+,使用

finishAffinity();完成亲和力();

For below 16, use对于 16 岁以下,使用

ActivityCompat.finishAffinity(YourActivity.this); ActivityCompat.finishAffinity(YourActivity.this);

Hope it helps!希望能帮助到你!

shareedit answered May 27 '18 at 8:03 shareedit 于 2018 年 5 月 27 日 8:03 回答

Akshay Taru阿克谢·塔鲁

When user click on the logout button then write the following code:当用户单击注销按钮时,请编写以下代码:

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

And also when after login if you call new activity do not use finish();并且在登录后,如果您调用新活动,请不要使用 finish();

Simply, when you go from the login screen, not when finishing the login screen.简单地说,当您从登录屏幕 go 时,而不是在完成登录屏幕时。

And then in all forward activities, use this for logout:然后在所有转发活动中,使用它来注销:

final Intent intent = new Intent(getBaseContext(), LoginScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);

It works perfectly.它完美地工作。

I found this way, it'll clear all history and exit我找到了这种方式,它将清除所有历史记录并退出

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Intent intent = new Intent(getApplicationContext(), SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

finish();
System.exit(0);

If you log in the user in screen 1 and from there you go to the other screens, use如果您在screen 1中登录用户并从那里 go 到其他屏幕,请使用

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

I found this solution to work on every device despite API level (even for < 11)尽管 API 级别(即使 < 11),我发现此解决方案适用于所有设备

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);

Best way to close all the previous activities and clear the memory关闭所有先前活动并清除 memory 的最佳方法

finishAffinity()
System.exit(0);

I have tried the flags on my end and still haven't worked.我已经尝试了我的标志,但仍然没有工作。 I have an application with a very similar design and I have a possible solution in terms of logic.我有一个设计非常相似的应用程序,并且在逻辑方面我有一个可能的解决方案。 I have built my Login and Logout using shared preferences.我已经使用共享首选项构建了我的登录和注销。

If I logout, data in my shared preferences is destroyed/deleted.如果我注销,我的共享首选项中的数据将被销毁/删除。

From any of my activities such as Home.java I check whether shared preferences has data and in this case it won't because I destroyed it when I logged out from one of the screens.从我的任何活动(例如 Home.java)中,我检查共享首选项是否有数据,在这种情况下它不会,因为我在从其中一个屏幕注销时将其销毁。 Therefore logic destroys/finishes that activity and takes me back to the Login activity.因此,逻辑会破坏/完成该活动并将我带回登录活动。 You can replicate this logic in all your other activities.您可以在所有其他活动中复制此逻辑。

However remember to perform this check inside onPostResume() because this is what is called when you go back to Home.java但是请记住在 onPostResume() 中执行此检查,因为这是当您 go 回到 Home.java 时调用的

Code Sample Below:下面的代码示例:

@Override
protected void onPostResume() {
    SharedPreferences pref = this.getSharedPreferences("user_details", Context.MODE_PRIVATE);
    if (pref.getAll().isEmpty()){
        //Shared Preferences has no data.
        //The data has been deleted
        Intent intent = new Intent(getApplicationContext(), Login.class);
        startActivity(intent);
        finish();
        //Finish destroys that activity which in our case is the Home.java
    }


    super.onPostResume();
}

In Kotlin this way:Kotlin这样:

in Another Activity (with some classes), under Imports在另一个活动(有一些类)中,在Imports

var activity:Activity?=null
    get() = field
    set(value) {
        field = value
    }

Then, under onCreate然后,在onCreate

activity=this

in MainActivity now:现在在 MainActivity 中:

activity?.finish()

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

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