Why does the main activity restart again after its called intent finished (Android 4.2.2)?

﹥>﹥吖頭↗ 提交于 2020-01-05 07:57:12

问题


The application works well in Android 2.3.5 But it does not work as desired in Nexus 4 (Android 4.2.2).

The application is: In the onCreate of main activity, it calls another activity through intent.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Intent launchIntent = new Intent(MainActivity.this, AndroidVideoCapture.class);
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(launchIntent);

    // I add a button dynamically here
}

Now with Nexus 4, after it finishes the intent of AndroidVideoCapture, it returns back to the begining of the onCreate, "setContentView(R.layout.activity_main);". So the intent restarts again and again.

Why?


回答1:


Try adding

if (savedInstanceState == null) {
    final Intent launchIntent = new Intent(MainActivity.this, AndroidVideoCapture.class);
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(launchIntent);
}

savedInstanceState not being null means the activity is being recreated.




回答2:


You can check if you have enabled 'Don't Keep Activities' in the developer options on the nexus 4 device. If checked, Disable that. This is an option available only since 4.0 and this is the reason behind your parent activity being 'recreated'.



来源:https://stackoverflow.com/questions/16424430/why-does-the-main-activity-restart-again-after-its-called-intent-finished-andro

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