سؤال

Let assume we have two activities.

A - main activity, that is "home launcher enabled" (proper intent filters etc.)

B - task root activity with singleTask specification (there can be only one instance of this activity) and with custom taskAffinity (to differentiate if from the main task root).

Let assume that B represents a task, that is valid only when it is not finished - returning to it or starting it again from recent tasks after finishing it is not an option.

In some point of the time - A starts B to begin a new task. The goal is to remove B from recent task list when user finishes B. User may navigate from B to other tasks (using home long press) and then navigate back to B as long as it is not finished. Launching A from launcher won't bring B to the foreground as they have different task affinities.

Android recognizes B as a root of a task, thus B is visible in the recent tasks list even if it is finished, and user can always go back to it. It is not a solution to move B to the one task with A, because in the time B is running - user should be able to switch between A and B tasks. Adding excludeFromRecents to B's manifest removes it completely from recent tasks list, and it is bad solution too.

How to achieve it? (sorry for my bad english)

هل كانت مفيدة؟

المحلول

Standard Answer

Sorry to be the bearer of bad news, but this functionality cannot be achieved utilizing standard methods without overriding the way that your app works. Even then, it will only work for as long as your app is running. Furthermore, doing so will require a rooted device with a specialize ROM because you cannot override the functionality of the Home button in anyway. All you can do is respond to onNewIntent() which does not fire if you are long pressing the Home button.

Recent Tasks is specifically for ease of use to the user to allow them to get back to whatever apps they have been using, regardless of whether the app is running. There is good news, however. This process is not keeping your app alive! It is simply an icon pointing to your package.

Unfortunately, this means that if your app is still running, something else is going on that is keeping it running.

Non-Standard Answer

It may be possible by editing the package properties during run-time (much in the same way that you can enable/disable components from java code). This is highly discouraged, because Recent Tasks is a usability feature of the Android core platform. This would require some research.

نصائح أخرى

A little late, but this still comes up as the top hit on Google. Please see this answer for a possible solution.

In your case you will have to:

  • Create a new Activity C called ExitActivity, and add manifest flags as described in the other answer.
  • Activity B should have the following attributes in the manifest: android:taskAffinity="com.example.ActivityB" android:allowTaskReparenting="true" android:launchMode="singleTask" android:documentLaunchMode="always"
  • Set the task affinity attribute on C to be the same as B
  • The call to Intent.addFlags in the ExitActivity should only be the last three: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top