android app按home键后重新进入,又重新打开app,如何破解?

版权声明:版权为博主所有,如需转载,请注明来源 https://blog.csdn.net/qq_38508087/article/details/84773533

前言:在开发时,相信大家也遇到过,我打开app,浏览了几个页面,然后点击home键回到了桌面,然后再次点击图标,理论上应该回到之前的页面,怎么会重新打开app呢?

        下面,请听我给大家分析一下,我在网上搜索过,五花八门,很多说在androidmainifest.xml中设置singletask等等,搞到最后把自己都搞懵逼了,其实home键只是相当于一个activity而已,当我们再次进入是判断一下,如果之前的activity存在,直接进入,如果不存在就重新开启,下面请看我是如何解决的:

       在BaseActivity中的setContentView()方法之前加上几行代码,或者在启动页的setContentView()方法之前加上也是一样的,因为我的activity进行了封装,所以放在了baseactivity里面,各位根据自己的实际情况,如下:

if (!isTaskRoot()) {
    final Intent intent = getIntent();
    final String intentAction = intent.getAction();
    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent
            .ACTION_MAIN)) {
        finish();
        return;
    }
}

亲测有效,如有什么疑问,请联系:[email protected],欢迎指正!

猜你喜欢

转载自blog.csdn.net/qq_38508087/article/details/84773533