Android app withdraw to the background, click the icon again to restart

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u014619545/article/details/88873357
After the app withdraw to the background, not killed, it is still running, but will restart after clicking the icon once again to re-create a series of pages

Project encountered this problem is affecting the user experience.
Solution:

APP in the startup page This code is added to onceate () method.

 if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

In fact, the reason is very simple, using the Installer program opens, there is no start of Intent with Category, while we ourselves are open procedure with a Category, so only the presence or absence of Category in the configuration Intent.ACTION_MAIN Activity judgment. Then put the notification bar when you want to generate the start of Intent presence or absence of Category.

Or launch page onceate in () method may use the following code:

// 避免从桌面启动程序后,会重新实例化入口类的activity
if (!this.isTaskRoot()) { // 当前类不是该Task的根部,那么之前启动
   Intent intent = getIntent();
   if (intent != null) {
      String action = intent.getAction();
      if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) { // 当前类是从桌面启动的
         finish(); // finish掉该类,直接打开该Task中现存的Activity
         return;
      }
   }
}

Choose one or more codes can.

thank:

http://www.cnblogs.com/net168/p/5722752.html

Guess you like

Origin blog.csdn.net/u014619545/article/details/88873357