1

In my application I have several "intents" that I use to transition between different activities in my application. I have noticed a strange behavior that occurs on Samsung devices - but not on Nexus devices - whenever a new intent is created the application launches a second "task" for this new activity! When the user goes to the multi-tasking menu they can see multiple copies of the application! This is not the desired behavior. Any and all advice would be greatly appreciated!

Manifest:

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

I have already attempted removing the launch modes as well as changing the application launch mode to singleTop and standard.

Intent that creates a second instance:

 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 that creates a third instance:

    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
}

A third instance can also be created from launching a settings 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);
}

I also over rode the onNewIntent Method:

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

Thank you very much for any help!!!

David Wasser
  • 93,459
  • 16
  • 209
  • 274
traintrax8
  • 69
  • 1
  • 10
  • Did you try only adding singleInstance only to the application? – MiltoxBeyond Jul 26 '16 at 19:31
  • Why do you need singleInstance launch mode? – Shaishav Jul 26 '16 at 19:32
  • The other option you have is moving over to the newer fragment lifecycle. The fragments can be replaced and added all on one activity – MiltoxBeyond Jul 26 '16 at 19:33
  • @shaishav Hello, I do not need it for any purpose - I added it in an attempt to resolve my issue – traintrax8 Jul 26 '16 at 19:34
  • They are used to make activities run in their own tasks which is exactly the thing you are trying to avoid. So, its better if you just remove them. – Shaishav Jul 26 '16 at 19:36
  • @MiltoxBeyond Hello, I just attempted to remove it everywhere except from the application and it appears to have worked!!! Thank you very much! Please write this as a solution and I will accept it! I still must test on actual hardware, however the problem disappeared in the Emulator! – traintrax8 Jul 26 '16 at 19:38
  • @Shaishav I did not know that! Now that I better understand what it does I more fully understand why these were not helpful. This is my first android application and I did not fully understand what they did even after googling it. Thank you for your assistance on my previous question as well! After removing them it appears to be working now - though I will need to test on actual hardware which I will do now! – traintrax8 Jul 26 '16 at 19:40
  • Glad to have helped. I figured it could be from trying to force each activity as a singleInstance. – MiltoxBeyond Jul 26 '16 at 19:41
  • On most devices you shouldn't see this behaviour. Standard Android behaviour is to ignore `singleTask` and `singleInstance` launch modes if the `Activity` being launched has the same `taskAffinity` as an existing task. In your case, even though you've specified `singleInstance` launch mode, Android should still launch new activities into the existing task and not create a new task. It looks like Samsung has modified the standard Android behaviour. – David Wasser Jul 27 '16 at 06:04
  • Possible duplicate of [Multiple Instances of Android Application open - ONLY on Touchwiz](https://stackoverflow.com/questions/38575165/multiple-instances-of-android-application-open-only-on-touchwiz) – TalkLittle Jun 17 '17 at 00:11

1 Answers1

2

Usually if you have to force a single instance of the app, you should avoid putting android:launchMode="singleInstance" on each activity seeing as it would try to launch an instance for each activity.

Removing the launchMode from everything except the application should ensure that only the application runs in a single instance, although what @Shaishav said is true, most of the time you can let android deal with the lifecycle of an application by not setting the launchMode, unless you have a real need to ensure only one instance is running at a time.

MiltoxBeyond
  • 2,683
  • 1
  • 13
  • 12
  • 2
    `android:launchMode` is not a valid attribute for the `` tag. It is ignored, and useless, and will probably confuse anyone who needs to maintain your code. Remove it. – David Wasser Jul 27 '16 at 05:58
  • We are supposed to use `intent-filter-new-task` now: https://wiki.appcelerator.org/display/guides2/Android+Intent+Filters#AndroidIntentFilters-LaunchMode – Yahya Uddin Aug 01 '18 at 21:32