为什么主要活动及其所谓的意图完成(4.2.2 Android版)后,再次重新启动? [英] Why does the main activity restart again after its called intent finished (Android 4.2.2)?

查看:206
本文介绍了为什么主要活动及其所谓的意图完成(4.2.2 Android版)后,再次重新启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用效果良好的的Andr​​oid 2.3.5
但是,根据需要在的Nexus 4(的Andr​​oid 4.2.2),这是行不通的。

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

中的应用是:
在主要活动的OnCreate,它通过意向调用另一个活动。

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
}

现在使用Nexus 4,它完成AndroidVideoCapture的意图后,它返回到的onCreate的开头,的setContentView(R.layout.activity_main);。
因此,意图一次又一次地重新启动。

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.

为什么?

推荐答案

尝试添加

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 不是空表示活动正在重建。

savedInstanceState not being null means the activity is being recreated.

这篇关于为什么主要活动及其所谓的意图完成(4.2.2 Android版)后,再次重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆