6

I have looking for the some suggestions for back to last opened activity when launch from app icon. But still has some side effects to handle.

LauncherActivity is the entry activity which listen for

<activity
            android:name="com.app.ui.LauncherActivity"
            android:configChanges="orientation|keyboardHidden"
            android:launchMode="singleTask"
            android:screenOrientation="nosensor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

Then there is another second activity and third activity.

My scenario is as below:

1. LauncherActivity -> SecondActivity 
2. SecondActivity -> ThirdActivity (When launching ThirdAcitivity, SecondActivity will finish itself)

So when at ThirdActivity, user click home key. If the app is opened from recentTask, then the last opened ThirdActivity will be shown. This is the behavior what i want.

But when the app is opened from the app icon shortcut, then it take to the LauncherActivity. But i want it to be the same behavior as recentTask. If the last opened ThirdActivity is not being destroyed, then it should show this one instead of LauncherActivity.

However if the app force stop and then restart or ThirdActivity is being destroyed, then no need to go back to ThirdActivity. Back to LauncherActivity is ok.

I have been looking for solution. The suggestion of saving last activity to sharedPreference is not good for my case.

I would like to know how to achieve like launch from recent task. Is any one can guide me. Thanks a lot.

rodent_la
  • 1,195
  • 4
  • 18
  • 38

4 Answers4

9

In your Launcher activity onCreate() method, write this:

if (!isTaskRoot()
            && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
            && getIntent().getAction() != null
            && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

        finish();
        return;
}

isTaskRoot() is true when there are no activities in activity stack. In your case, if another activity (Third activity in your case) is there in Activity stack, above code will finish the launching activity and will bring Third Activity to screen.

DsD
  • 1,081
  • 8
  • 11
  • LauncherActivity is a singleTask. onCreate() will not be called if it is already in the task. – rodent_la Jul 19 '16 at 06:20
  • Your launcher activity wont be in the task because while moving to another activity, you'll finish it. When you will leave the application, the activity where you left the app will be there in task. isTaskRoot will return false if it finds this last left activity in task, and will finish newly launched LauncherActivity after clicking on app icon and will bring last left activity to front. – DsD Jul 19 '16 at 07:33
  • Awesome! Stumbled many days on that. I needed only one instance of launcher activity whenever it was run from. It can be easily achieved by singleTop attribute. But your code lets avoid not bringing it on top if there was another activities in the task. I think should be an intent flag for this behavior but there is none. – RoK Oct 28 '20 at 12:20
  • keep in mind that this will only prevent opening the app again from launcher, but not from other kind of intents. – htafoya Mar 24 '22 at 17:52
  • What if i dont want to finish the launcher activity? In this case we deliberately finish the launcher – abhishek maharajpet Jul 11 '22 at 06:45
0

You can handle this by having three fragments in one activity. Let's that activity be your LaunchActivity, then it has LAUNCHER intent-filter and always opens when you click app icon. If you had three fragments in that activity then the last fragment is opening just like when you open your app from recent tasks.

hadilq
  • 1,023
  • 11
  • 25
0

You could also remove launchMode="singleTask". Most apps don't need it.

Lev
  • 6,487
  • 6
  • 28
  • 29
0

If you still want to make your launcher a singleTask you have to do this hack. Let's say your existing Launcher is Activity A (with Single Task)

  1. Create a new Activity let say "LauncherActivity" make it as LAUNCHER instead of Activity A, for LauncherActivity don't add any launch mode.

  2. Inside that Launcher activity onCreate() you do nothing except Launching a "Activity A" and finishing Launcher Activity.

That's it now. You you have achieved everything you wanted.

  1. Single Task behaviour of your Activity A(No Duplicate instances)
  2. State is maintained, that is when you come via Icon click Last activity state will be resume. Why? because your LauncherActivity is not a "singleTask"
Dhruv Kaushal
  • 632
  • 8
  • 17