3
 <activity
            android:name=".activity.landingpage.LandingPageActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

So its simple. When i open my app from launcher icon, it opens.I go and click on launcher my app icon again it brings my application to the front again since its open.However, If I go to the play store and click open from there my app has two instances open. If you want an example how working, facebook has same issue .also another app fotmob has same issue i think.

user3278732
  • 1,694
  • 10
  • 31
  • 67

1 Answers1

11

I had same issue. Put below code to your launcher Activity.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     if (!isTaskRoot()
                && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
                && Intent.ACTION_MAIN.equals(getIntent().getAction())) {
            finish();
            return;
        }
     //other code
}

Problem:

Suppose you have task stack [A -> B -> C] and A is root Activity, when you launch application from play store task stack will become(system clears the top of root) [A] but if you have finished your root Activity stack will look like(system will create new instance of root Activity and place top of the existing task) [B -> C -> A].

Why?

Because play store will launch app with category=LAUNCHER, action=MAIN, flag = ACTIVITY_NEW_TASK, flag = ACTIVITY_BROUGHT_TO_FRONT

Solution

Take a case that you have removed your root activity and now your stack is [B -> C], when you try to open app from play store your stack become [B -> C -> A]

now, A is not in root of the stack and has category = LAUNCHER and action = MAIN so, we can remove A using finish() and stack will remain same [B -> C]

*Note: Tested on android 7.0

Pratik Popat
  • 2,891
  • 20
  • 31
  • would you mind elaborate a bit about why this solve the problem? – lelloman Jul 26 '17 at 10:36
  • Thank You for this answer, it solved the problem in hand. Funny part, lots of apps have this particular error.They could use here solution. – user3278732 Jul 26 '17 at 10:44
  • My all-time favourite Android bug :-( Still hasn't been fixed since the dawn of time. See https://stackoverflow.com/a/16447508/769265 – David Wasser Jul 27 '17 at 14:12
  • The solution in your answer is correct and will solve the problem. Your explanation, however, is wrong. The bug exists when you start the app (for the first time) from the installer or (sometimes, depending on how it is done) from an IDE like IntelliJ IDEA or Android Studio or Eclipse. Once you launch the app in the this way, if you press HOME and launch the app by clicking on the app icon, Android is confused and does not recognize that the launch `Intent` should just bring the task to the foreground, so it creates a new instance of the launch `Activity` when it shouldn't. – David Wasser Jul 27 '17 at 14:28
  • See https://issuetracker.google.com/issues/36941942 and https://issuetracker.google.com/issues/36907463 also (please star them as it will help to raise awareness) – David Wasser Jul 27 '17 at 14:29
  • @DavidWasser I don't know whether this solution and explanation is applicable for all device and condition, this is the thing what I have analyzed using android emulator version 7.0. I tried to find document about brought_to_front flag but it is for system so document not available about it. – Pratik Popat Jul 28 '17 at 03:42
  • @DavidWasser if you have correct or better analysis you can edit the answer. – Pratik Popat Jul 28 '17 at 03:43
  • Great it's Save me – Tanveer Munir Oct 22 '19 at 07:24