Symptom After the Application is installed with the Android installer, click to open the application and press the home button to cut the background and click icon to start, causing the root activity to start repeatedly. Application exceptions and delays may occur.

why

Welcome to the ever-growing list of users who have been bitten by this one. This is a well-known and long-standing Android bug. in the way applications get launched the first time from the installer, web-browser and via IDE (IntelliJ, Eclipse, etc.). See these issues filed long ago related to the problem: http://code.google.com/p/android/issues/detail?id=2373 http://code.google.com/p/android/issues/detail?id=26658 It is still broken and you cannot prevent this from happening. The only thing you can do is to detect when Android has launched a second instance of your root activity into an existing task. You can do this by putting this code in onCreate() of your root activity:

Add this sentence to the root activity onCreate().

if(! isTaskRoot()) { // Android launched another instance of the root activity into an existing task // so just quietly finish and go away, dropping the user back into the activity // at the top of the stack (ie: the last state of this task) finish();return;
}
Copy the code
/**
 * Return whether this activity is the root of a task.  The root is the
 * first activity in a task.
 *
 * @return True if this is the root activity, else*/ // Returns whether the current activity is the root activity of the task, which means it is the first activity in the task. Public BooleanisTaskRoot() {
    try {
        return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) > = 0; } catch (RemoteException e) {return false; }}Copy the code

Reference link: https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508#1644 7508

Related links: https://blog.csdn.net/cppalien/article/details/41958767 https://blog.csdn.net/u013039472/article/details/53284312 https://blog.csdn.net/singwhatiwanna/article/details/9294285

Appendix: The following is some exception log information,

You can always see the current application’s Activity stack by using the following command

adb shell dumpsys activity activities | grep com.package.name
Copy the code
Activities=[ActivityRecord{232df185 u0 com.package.name/.ui.welcome.LaunchActivity t135}, ActivityRecord{328bcab9 u0 com.package.name/.ui.main.MainActivity t135}, ActivityRecord{3ef044dc u0 com.package.name/.ui.room.RoomActivity t135}, ActivityRecord{339781a4 u0 com.package.name/.ui.show.ShowActivity t135}, ActivityRecord{15a663ed u0 com.package.name/.ui.welcome.LaunchActivity t135}]
      * Hist #4: ActivityRecord{15a663ed u0 com.package.name/.ui.welcome.LaunchActivity t135}
          packageName=com.package.name processName=com.package.name
          app=ProcessRecord{31ce96e8 16228:com.package.name/u0a148}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10600000 cmp=com.package.name/.ui.welcome.LaunchActivity (has extras) }
          frontOfTask=false task=TaskRecord{2d6e9434 #135 A=com.package.name U=0 sz=5}
          taskAffinity=com.package.name
          realActivity=com.package.name/.ui.welcome.LaunchActivity
          baseDir=/data/app/com.package.name-1/base.apk
          dataDir=/data/data/com.package.name
      * Hist #3: ActivityRecord{339781a4 u0 com.package.name/.ui.show.ShowActivity t135}
          packageName=com.package.name processName=com.package.name
          launchedFromUid=10148 launchedFromPackage=com.package.name userId=0
          app=ProcessRecord{31ce96e8 16228:com.package.name/u0a148}
          Intent { cmp=com.package.name/.ui.show.ShowActivity (has extras) }
          frontOfTask=false task=TaskRecord{2d6e9434 #135 A=com.package.name U=0 sz=5}
          taskAffinity=com.package.name
          realActivity=com.package.name/.ui.show.ShowActivity
          baseDir=/data/app/com.package.name-1/base.apk
          dataDir=/data/data/com.package.name
      * Hist #2: ActivityRecord{3ef044dc u0 com.package.name/.ui.room.RoomActivity t135}
          packageName=com.package.name processName=com.package.name
          launchedFromUid=10148 launchedFromPackage=com.package.name userId=0
          app=ProcessRecord{31ce96e8 16228:com.package.name/u0a148}
          Intent { cmp=com.package.name/.ui.room.RoomActivity (has extras) }
          frontOfTask=false task=TaskRecord{2d6e9434 #135 A=com.package.name U=0 sz=5}
          taskAffinity=com.package.name
          realActivity=com.package.name/.ui.room.RoomActivity
          baseDir=/data/app/com.package.name-1/base.apk
          dataDir=/data/data/com.package.name
      * Hist #1: ActivityRecord{328bcab9 u0 com.package.name/.ui.main.MainActivity t135}
          packageName=com.package.name processName=com.package.name
          launchedFromUid=10148 launchedFromPackage=com.package.name userId=0
          app=ProcessRecord{31ce96e8 16228:com.package.name/u0a148}
          Intent { flg=0x4000000 cmp=com.package.name/.ui.main.MainActivity }
          frontOfTask=false task=TaskRecord{2d6e9434 #135 A=com.package.name U=0 sz=5}
          taskAffinity=com.package.name
          realActivity=com.package.name/.ui.main.MainActivity
          baseDir=/data/app/com.package.name-1/base.apk
          dataDir=/data/data/com.package.name
      * Hist #0: ActivityRecord{232df185 u0 com.package.name/.ui.welcome.LaunchActivity t135}
          packageName=com.package.name processName=com.package.name
          app=ProcessRecord{31ce96e8 16228:com.package.name/u0a148}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.package.name cmp=com.package.name/.ui.welcome.LaunchActivity }
          frontOfTask=true task=TaskRecord{2d6e9434 #135 A=com.package.name U=0 sz=5}
          taskAffinity=com.package.name
          realActivity=com.package.name/.ui.welcome.LaunchActivity
          baseDir=/data/app/com.package.name-1/base.apk
          dataDir=/data/data/com.package.name
      TaskRecord{2d6e9434 #135 A=com.package.name U=0 sz=5}
        Run #9: ActivityRecord{15a663ed u0 com.package.name/.ui.welcome.LaunchActivity t135}
      TaskRecord{2d6e9434 #135 A=com.package.name U=0 sz=5}
        Run #7: ActivityRecord{339781a4 u0 com.package.name/.ui.show.ShowActivity t135}
        Run #6: ActivityRecord{3ef044dc u0 com.package.name/.ui.room.RoomActivity t135}
        Run #5: ActivityRecord{328bcab9 u0 com.package.name/.ui.main.MainActivity t135}
        Run #4: ActivityRecord{232df185 u0 com.package.name/.ui.welcome.LaunchActivity t135}
    mResumedActivity: ActivityRecord{15a663ed u0 com.package.name/.ui.welcome.LaunchActivity t135}
  mFocusedActivity: ActivityRecord{15a663ed u0 com.package.name/.ui.welcome.LaunchActivity t135}
Copy the code

There should not be more than one LaunchActivity. The intent is launched for the first time

Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.package.name cmp=com.package.name/.ui.welcome.LaunchActivity }
Copy the code

Flag is FLAG_ACTIVITY_NEW_TASK 0x10000000.

/**
 * If set, this activity will become the start of a new task on this
 * historystack. A task (from the activity that started it to the * next task activity) defines an atomic group of activities that  the * user can move to. Tasks can be moved to the foreground and background; * all of the activities inside of a particular task always remainin
 * the same order.  See
 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
 * Stack</a> for more information about tasks.
 *
 * <p>This flag is generally used by activities that want
 * to present a "launcher" style behavior: they give the user a list of
 * separate things that can be done.which otherwise run completely
 * independently of the activity launching them.
 *
 * <p>When using this flag, if a task is already running for the activity
 * you are now starting, then a new activity will not be started; instead,
 * the current task will simply be brought to the front of the screen with
 * the state it was last in.  See {@link #FLAG_ACTIVITY_MULTIPLE_TASK} for a flag
 * to disable this behavior.
 *
 * <p>This flag can not be used when the callerIs requesting a result from * the activity being launched. */ / If this is set,a new task will be launched in the history stack, and the activity is the first element of this task. Tasks (from the current activity // start to the next task) define a subgroup of activities that the user can move. Tasks can be toggled from the front background. // All activities in the specified task are always in the same order. // This flag is usually used when an activity wants to appear in the form of a launcher; They give users a separate list, otherwise run their activities completely independently with // starting them. // When using this flag, if a task is already running the activity you are starting, a new activity will not start; Instead, the current task only needs to run to the foreground with the last state. // This flag cannot be used when the caller wants to get results from the currently started activity. public static final int FLAG_ACTIVITY_NEW_TASK = 0x10000000;Copy the code

The second time you launch the LaunchActivity, the intent is:

Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10600000 cmp=com.package.name/.ui.welcome.LaunchActivity (has extras) }
Copy the code

falg=FLAG_ACTIVITY_BROUGHT_TO_FRONT&FLAG_ACTIVITY_RESET_TASK_IF_NEEDED&FLAG_ACTIVITY_NEW_TASK

/**
 * This flag is not normally set by application code, but set for you by
 * the system as described in the
 * {@link android.R.styleable#AndroidManifestActivity_launchMode
 * launchMode} documentation forThe singleTask mode. */ /** * This flag does not normally need to be set by application code, but is set by the system in singleTask mode. */ public static final int FLAG_ACTIVITY_BROUGHT_TO_FRONT = 0x00400000; /** * Ifset, and this activity is either being started in a new task or
 * bringing to the top an existing task, then it will be launched as
 * the front door of the task.  This will result in the application of
 * any affinities needed to have that task in the proper state (either
 * moving activities to or from it), or simply resetting that task to
 * its initial state if needed.
 */
public static final int FLAG_ACTIVITY_RESET_TASK_IF_NEEDED = 0x00200000;
Copy the code