2

I have been working on an application for Android devices recently - and I have noticed a perplexing issue that only occurs on devices running Samsung Touchwiz!

When the application is running on a Touchwiz device the bug occurs. The bug can be reproduced by pressing the "back" button while the application is in the foreground - and then launching it again from the home screen (or anywhere else the icon may be). Looking in the multi-tasking menu it is clear that the system launches a second instance of the application! This second instances is totally independent from the first instance and the two do not seem to be connected in any way.

I thought I could prevent this behavior by adding singleInstance to the applications Manifest, but this did not appear to do the trick. 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">

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

    </activity>
    <activity
        android:name=".Settings_area"
        android:screenOrientation="portrait" />
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <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>

It is interesting to note that the second instance "Freezes" at the applications splash screen - until this second instance is clicked from the multi - tasking menu.

This is how I am handling the splash screen:

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

I also over-rode the back buttons action in my main activity:

public void onBackPressed()
{
    moveTaskToBack(true);
}

This bug only occurs on devices with TouchWiz. I have tested my application on several devices and this bug is not able to be reproduced on any device except for those Samsung devices running TouchWiz.

Any suggestions will be greatly appreciated.

Thank you very much!

traintrax8
  • 69
  • 1
  • 10
  • 1
    did you try killing the process on back pressed ? if not try this android.os.Process.killProcess(android.os.Process.myPid()); – Muthu Jul 25 '16 at 19:16
  • Hello @Muthu , I do not wish to terminate the process if the user presses the back button - rather to simply send it to the background! – traintrax8 Jul 25 '16 at 19:31
  • 1
    can you remove intent-filter from MainActivity and check? – Muthu Jul 25 '16 at 19:44
  • @muthu it still occurs even when i remove the intent-filter from the main activity. Removing the intent filter from the splash screen (and changing the mainActivity to ...category.LAUNCHER resolves the problem BUTcauses the application to crash if the user presses the back button and then presses the icon on the home screen. Thanks for helping! – traintrax8 Jul 25 '16 at 19:51
  • did it solve the issue? – Muthu Jul 25 '16 at 19:53
  • @muthu it created a new issue - the application now crashes if the user attempts to launch the app while it is already running - ONLY on Touchwiz devices however. – traintrax8 Jul 25 '16 at 19:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118261/discussion-between-traintrax8-and-muthu). – traintrax8 Jul 25 '16 at 19:56
  • can you add the stacktrace of the crash ? – Muthu Jul 25 '16 at 19:56

1 Answers1

1

The issue is seems to be with the intent filters in mainactivity. remove the intent filters from mainactivity that will solve the issue.

Muthu
  • 1,022
  • 1
  • 7
  • 17