After Home button press re-launching app shows initial activity not current

后端 未结 6 1470
不知归路
不知归路 2021-01-04 04:12

I have an application that has an initial activity (A - also the one specified in the launcher) from which it allows the user to launch another activity (B). I am saving the

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 04:45

    This is because another instance of app is launching when icon is pressed.

      // To prevent launching another instance of app by clicking app icon 
                    if (!isTaskRoot()
                            && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
                            && getIntent().getAction() != null
                            && getIntent().getAction().equals(Intent.ACTION_MAIN)) {
    
                        finish();
                        return;
                    }
    

    write the above code in your launcher activity before calling setContentView. This will solve the problem

提交回复
热议问题