0

I understand that launchMode:"singleTask" means that if an activity has previously been created and is requested in future to show up , Android will simply show the old one instead of creating a new instance. The problem i am facing can be illustrated with an example :-

Lets Say we have two activities
A actvity
B (Activity with launchMode="singleTask") enabled (This is all that is in its Manifest Declaration)

Now , if A creates an Intent which corresponds to B (A->B) , then A gets Finished and is removed from the activity stack . How can i prevent this from happening . What i want is that when A calls B (Creates an Intent) previous instance of B is returned and A remains as it is and is not destroyed . Thank you for helping . Any help is appreciated

EDIT

<activity
        android:name="com.example.Activities.A"
        android:launchMode="singleTask"
        android:clearTaskOnLaunch="false"
        android:finishOnTaskLaunch="false"
        android:label="@string/app_name"

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

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.Activities.B"
        android:launchMode="singleTask"
        android:clearTaskOnLaunch="false"
        android:finishOnTaskLaunch="false"
        android:label="@string/app_name"

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

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
David Wasser
  • 93,459
  • 16
  • 209
  • 274
Sarthak Mishra
  • 1,033
  • 1
  • 11
  • 27
  • I don't believe, from what I understand, that what you want is possible. Perhaps you need to rethink what it is that needs to be kept and consider other ways of keeping it. However, what if B were to start, know that A hasn't start A which then returns to B? – MikeT Jun 04 '16 at 06:34
  • Could you please suggest me a solution , i have tried almost everything . Thanks! – Sarthak Mishra Jun 04 '16 at 06:35
  • Above comment amended to make it clearer. Sorry ran out of time. Perhaps you need to rethink what it is that needs to be kept and consider other ways of keeping it. However, what if B were to start, know that A hasn't been started, immediately start A (i.e B just basically flags A as now having been started), which then returns to B? – MikeT Jun 04 '16 at 06:39
  • The obvious solution is to not use `singleTask` or `singleInstance` and work with `standard`. Why couldn't that be possible? – MikeT Jun 04 '16 at 06:43
  • Because standard would always create a new instance of any activity to which an intent is made. That is something i dont want. I want to get the previous instance – Sarthak Mishra Jun 04 '16 at 06:45
  • Could you just suggest me a combination of flags . PS . I tried FLAG_ACTIVITY_REORDER_TO_FRONT ..and that froze my device – Sarthak Mishra Jun 04 '16 at 06:49
  • Have you checked whether `finishOnTaskLaunch` (false) or (unlikely and) `allowTaskReparenting` could do accomplish you want? That's all I can think of. I assume that you've looked at – MikeT Jun 04 '16 at 06:57
  • In the manifest activity – Sarthak Mishra Jun 04 '16 at 07:23
  • Yep. Have a look at [](https://developer.android.com/guide/topics/manifest/activity-element.html) – MikeT Jun 04 '16 at 07:33
  • No Luch :( , I ve posted the activity declaration from my manifest could have a look maybe something can be done there – Sarthak Mishra Jun 04 '16 at 07:40
  • What i want is basically that if we have two activities and one calls another then the previous instance should be opened and not a new activity . Could you tell me how can i achieve that – Sarthak Mishra Jun 04 '16 at 07:59

1 Answers1

0

If you just want to have 2 activities that switch back and forth in the same task, then you should remove all special launch modes and just launch one activity from the other like this:

Intent intent = new Intent(this, ActivityX.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

If you start B from A using this method and also start A from B using this method, then you will have one instance of each Activity in your task and they will just change positions in the stack. This won't create any new instances.

Also remove the <intent-filter> declarations, you don't need them. You only need one for the root Activity which should contain "ACTION=MAIN" and "CATEGORY=LAUNCHER".

David Wasser
  • 93,459
  • 16
  • 209
  • 274