lixplor / android-Q-A

🐞 android related questions and answers
0 stars 0 forks source link

安装apk后直接启动app, 进入某个页面按小房子返回桌面, 然后点击launcher图标会重复进app, 而退出app后再次打开没有这种情况. #43

Closed lixplor closed 7 years ago

lixplor commented 7 years ago

安装apk后直接启动app, 进入某个页面按小房子返回桌面, 然后点击launcher图标会重复进app, 而退出app后再次打开没有这种情况.

lixplor commented 7 years ago

原因: 安装完后点击的启动和launcher启动并不是同一个栈 解决: http://blog.csdn.net/devilnov/article/details/50233423, 将以下代码放入launcher的activity的oncreate方法中

//判断该Activity是不是任务空间的源Activity,“非”也就是说是被系统重新实例化出来  
if(!this.isTaskRoot()) { 
    //如果你就放在launcher Activity中话,这里可以直接return了  
    Intent mainIntent=getIntent();   
    String action=mainIntent.getAction();  
    if(mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {  
        finish();  
        //finish()之后该活动会继续执行后面的代码,你可以logCat验证,加return避免可能的exception  
        return;
    }  
}