After tap on app icon,launcher create a new instance of root activity again & again.

房东的猫 提交于 2019-12-11 01:59:10

问题


After tap on app icon,launcher create a new instance of root activity & onCreate() of root activity is called when root activity already present in background i.e onPause(). It creating a new instance of root activity again & again instead of onResume() root activity.

I know This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

After long searching I have found the following code It is working.but I think it is workaround.Is there any other solution except following code.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)     != 0) {
        // Activity was brought to front and not created,
        // Thus finishing this will get us to the last viewed activity
        finish();
        return;
    }

    // Regular activity creation code...
}

来源:https://stackoverflow.com/questions/28043865/after-tap-on-app-icon-launcher-create-a-new-instance-of-root-activity-again-ag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!