android - 每个 Intent 都会在 Android 应用程序中启动一个新任务 - 如何防止?

标签 android android-intent multiple-instances

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

list :

 <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

创建第二个实例的 Intent:

 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;
            }

创建第三个实例的 Intent :

    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
}

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

    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**)
    }

非常感谢您的帮助!!!

最佳答案

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

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

关于android - 每个 Intent 都会在 Android 应用程序中启动一个新任务 - 如何防止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38598362/

相关文章:

android - 如何在点击后、新 Intent 之前加载插页式广告?

android - 使用 fragment 处理 setDisplayHomeAsUpEnabled

c# - 如何将 Citrix 中的 C# 程序实例限制为每个用户 1 个

apache - 如何在 RedHat Linux 中运行两个 Apache Web 服务器 (httpd)?

C++,dll的多个实例,单例

android - Json 到 Android 中的 POJO 映射

android - 为什么我的 Intent 在我的应用程序的设置中打开应用程序信息,而不是我传递给它的 Activity 类?

android - 如何在android中调用搜索框?

android - 从构建中删除 AOSP 应用程序

java - Android 上下文和共享首选项