每个意图都在Android应用程序中启动一项新任务 - 如何预防?

时间:2016-07-26 19:28:38

标签: android android-intent multiple-instances

在我的应用程序中,我有几个“意图”,我用它来在我的应用程序中的不同活动之间进行转换。我注意到三星设备上发生了一种奇怪的行为 - 但是在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**)
    }

非常感谢您的帮助!!!

1 个答案:

答案 0 :(得分:2)

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

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