【问题标题】:Every intent starts a new task in Android App - how to prevent?每个意图在 Android 应用程序中启动一个新任务 - 如何防止?
【发布时间】:2016-07-27 14:00:31
【问题描述】:

在我的应用程序中,我有几个“意图”用于在应用程序中的不同活动之间进行转换。我注意到三星设备上发生了一种奇怪的行为 - 但不是在 Nexus 设备上 - 每当创建新意图时,应用程序都会为这个新活动启动第二个“任务”!当用户进入多任务菜单时,他们可以看到应用程序的多个副本!这不是期望的行为。任何和所有的建议将不胜感激!

清单:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:launchMode="singleInstance">
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:launchMode="singleInstance">
    </activity>
    <activity
        android:name=".Settings_area"
        android:screenOrientation="portrait" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyDieXTCaFoIL0kJ_IM4UMBSQL3sNn92AWM" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" />
    <activity android:name=".Splash"
        android:launchMode="singleInstance">


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".aboutPageActivity" />
    <activity android:name=".turnOffFromNotification"
        android:noHistory="true"></activity>
</application>

我已经尝试删除启动模式,并将应用程序启动模式更改为singleTopstandard

创建第二个实例的意图:

 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                new Handler().postDelayed(new Runnable(){
                    @Override
                    public void run() {
            /* Create an Intent that will start the Menu-Activity. */
                        Intent mainIntent = new Intent(Splash.this,MainActivity.class);
                        Splash.this.startActivity(mainIntent);
                        Splash.this.finish();
                    }
                }, splashDisplayLength);
                return;
            }

创建第三个实例的意图:

    public void goToAboutPage()
{
    Intent goToAboutPage = new Intent(this, aboutPageActivity.class); //create the intent to go to the map screen
    startActivity(goToAboutPage); //actually go to the map screen
}

第三个​​实例可以通过启动设置意图来创建:

    public void changeToSettingsScreen() //changes the screen to the setting screen
{
    readyToSendPackets = false;
    sendSwitch.setChecked(false);
    //  textView.setText("NOT sending"); //set the textview to advise users packets are not being sent
    Intent goToSettings = new Intent(this, Settings_area.class);
    startActivity(goToSettings);
}

我也过度使用了 onNewIntent 方法:

    protected void onNewIntent(Intent intent) {
  //  super.onNewIntent(intent); //REMOVED THIS TO AVOID DOUBLE INSTANTIATION ON TOUCHWIZ IF ANYTHING BREAKS LOOK HERE FIRST
    setIntent(intent); //this allows us to recieve the  extras bundled with the intent
    // System.out.println("Here is the bindle: " +  getIntent().getExtras());
    if (getIntent().getExtras() != null) //check to see if there are any extras, there wont be on apps first start
    {
        Bundle extras = getIntent().getExtras(); //get the extras
        String methodName = extras.getString("methodName"); //assign the extras to local variables

        if(methodName != null && methodName.equals("turn_send_switch_off"))
        {
            sendSwitch.setChecked(false);
        }
        //else if(**other actions that may need to be performed can go here**)
    }

非常感谢您的帮助!!!

【问题讨论】:

  • 您是否尝试仅将 singleInstance 添加到应用程序中?
  • 为什么需要单实例启动模式?
  • 您拥有的另一个选择是转移到更新的片段生命周期。片段可以在一个活动中全部替换和添加
  • @shaishav 你好,我不需要它用于任何目的 - 我添加它是为了解决我的问题
  • 它们用于使活动在自己的任务中运行,这正是您要避免的事情。因此,最好将它们删除。

标签: android android-intent multiple-instances


【解决方案1】:

通常,如果您必须强制应用程序的单个实例,则应避免将android:launchMode="singleInstance" 放在每个活动上,因为它会尝试为每个活动启动一个实例。

从除应用程序之外的所有内容中删除launchMode 应该确保只有应用程序在单个实例中运行,尽管@Shaishav 说的是真的,大多数时候你可以让android 处理应用程序的生命周期,而不是设置launchMode,除非您确实需要确保一次只运行一个实例。

【讨论】:

  • android:launchMode 不是 &lt;application&gt; 标记的有效属性。它被忽略且无用,并且可能会使需要维护您的代码的任何人感到困惑。删除它。
  • 我们现在应该使用intent-filter-new-taskwiki.appcelerator.org/display/guides2/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-21
相关资源
最近更新 更多