问题
There are many question here about this topic, but none of the answers works for me. I am programming an app that will carry sensitive data, and for such reason I want to make the user login each time it enters the app. I already tried the singletask launch mode and clearTaskOnLaunch, also I hide the task from the "Recents Apps" with the exclude from recents attribute.
But now the app don't behaves as it should with the Back button.
Example:
LoginActivity > MainActivity > HelpActivity
Now the user presses the Home Button, since the app is not on the recents list the user will need to use the launcher icon. LoginActivity pops up as expected but, if the user presses the Back Button it goes to Helpactivity instead of exitingn the app.
Edit: I already found the solution, the problem was that I was calling finish() on the LoginActivity after starting MainActivity. So when clicking on the launcher the Android system was re-parenting the Activities putting the new instantiated LoginActivity at the top of the stack, instead of clearing it leaving only the root Activity since there was not such Activity.
回答1:
Have you tried noHistory option in manifest?
Also you can implement this by the use of SharedPreferences:
public void onPause(){
super.onPause();
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP. ", Toast.LENGTH_SHORT).show();
//Here you can maintain a boolean value in sharedpreferences stating everytime you leave and login into the app.
}
}
}
Here is the link : click on home key and application run in background
来源:https://stackoverflow.com/questions/24864524/android-clear-always-the-back-stack