search

Home  >  Q&A  >  body text

Android怎么实现从桌面点击图标后返回上一次退出该应用的状态

android应用默认好像是每次点击桌面图标都是启动的主Activity,现在我在APP中跳转到某个界面后直接按Home键后退到了桌面,我想再次点击图标直接跳转到上次退出时的那个Activty且状态与退出时完全一致,要怎么来实现哦!

测试最奇怪的就是在开发环境下launch运行到手机中,使用Home返回桌面,再次点击应用程序就是可以返回之前的状态的,多次测试都是正常的;但是当我打包签名之后,发送到手机上使用这个签名的APK来测试就会出现每次点击应用程序图标都会运行MainActivity,之前的界面就在这个MainActivity之下,即结束这个MainActivty就会出现之前的界面。
启动多少次就要多少了MainActivity,我也是醉了啊~~~~就是返不回之前的状态。

伊谢尔伦伊谢尔伦2772 days ago671

reply all(6)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 17:39:54

    Is the interface you jump to in your app? You need to take a look at the four startup modes of Activity, which will be helpful to you.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:39:54

    Have you turned on "Don't keep background tasks" on your phone?

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:39:54

    Under normal circumstances, clicking the icon again will jump directly to the activity you exited last time. However, there are also some abnormal situations. For example, the Android system thinks that the memory is not enough, so it cleans up some activities that occupy more memory.
    In this case, the system itself will not restore it to you.
    If you really want to jump to this state again, you can only rely on yourself to record the working status of the app. After restarting, read the status and jump automatically.

    reply
    0
  • 阿神

    阿神2017-04-17 17:39:54

    You can set singletask=true in the main activity of the app. In theory, click the app icon to restart the app and enter the main activity of the app. This is a push operation. When you click back to exit this startup, Activity has been popped out of the stack again.
    At this time, if you pressed the home button last time when running this app to switch this app instance to a background task, the app that was switched to the background when you last pressed the home button will pop up. The example should be the situation you describe.

    This needs to be solved. Theoretically, set the singletask of the entry activity to true. When you click the app icon to start the app, the manager should check whether there is a running instance of the app based on the signature of the app. If so, switch to the foreground task. If not, restart.

    It should be a problem of program single instance.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:39:54

    This is a bug in Android. It will not appear after rooting and installing silently.
    Solution, add

    in onCreate of your mainActivity
    `if (!isTaskRoot()) {
        finish();
        return;
    }`

    reply
    0
  • 阿神

    阿神2017-04-17 17:39:54

    The reason why this event is triggered is: after installing the program, click to open it, click the HOME button to return to the desktop, and then click the icon to enter the program. The reason should be that the information carried by the intent is different. Solution:

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //这是为了应用程序安装完后直接打开,按home键退出后,再次打开程序出现的BUG
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {  
        finish();
        return;  
    }        

    }

    reply
    0
  • Cancelreply