2

I have an activity that I want to exist only once, no matter how many times it is opened. Previously I was using launchMode singleTask, and everything was working great. However, now I need to be using onActivityResult, which for some reason is incompatible with launchMode singleTask.

Do you know of a way to replicate what either of them does.

ade.se
  • 1,644
  • 1
  • 11
  • 11
Leo
  • 4,652
  • 6
  • 32
  • 42
  • "However, now I need to be using onActivityResult, which for some reason is incompatible with onActivityResult" -- that sentence does not parse properly. Also, why do you think you need `singleTask`, and why do you think you need `startActivityForResult()`/`onActivityResult()`? If you would take the time to explain what you are trying to achieve, it will be easier for people to suggest alternative solutions. – CommonsWare Sep 30 '11 at 23:27
  • Ok. I need startActivityForResult() and onActivityResult() because I am launching a detail view, and need results from there to propagate into my original activity. Basic premise of the question, is that I need both what onActivityResult does and what singleTask accomplish. I have implemented a workaround(hack), wherein I am using singleTask, but handling the results from activity myself in onResume with Global Variables. – Leo Sep 30 '11 at 23:56
  • 1
    Why do you think you need `singleTask`? – CommonsWare Sep 30 '11 at 23:58
  • Without singleTask, what ends up happening is that on back button being pressed you just go to old instances of the same activity multiple times, this is not desired at all. – Leo Oct 01 '11 at 00:03
  • Then this is not an activity that should be used with `startActivityForResult()`, as the whole **point** behind `startActivityForResult()` is that the user chooses something in the spawned activity and then that activity calls `finish()` and goes away. This means there never are multiple instances. Hence, for a well-designed app, `startActivityForResult()` and `singleTask` are mutually exclusive, not only technically, but in terms of UX. – CommonsWare Oct 01 '11 at 00:31
  • @CommonsWare I understand this point of view, but it raises a challenge for my application. I have three activities: A -> B -> C. C should only have one instance, and can either return success or cancel. Success should return to activity A, while cancel should return to activity B. How can I accomplish this without dropping `singleTask`? – Paul Lammertsma Nov 06 '11 at 20:33

2 Answers2

3

My Main activity is also a singleTask but I'm certainly startingActivitiesForResults from my Main activity and getting back results.

Here is my manifest declaration:

<activity 
    android:name=".activities.Main"  
    android:theme="@android:style/Theme.Light"
    android:screenOrientation="portrait" 
    android:launchMode="singleTask" 
    > 
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Here is my onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    // blah blah checking if if the requestCode and resultCode are good //
}

I'm sure you're setting the result in the other Activities...

So just cross check your code with mine, let's see if we can find the problem.

  • make sure onActivityResult is "protected" when you're overriding it.

Hope this helps

-serkan

serkanozel
  • 2,947
  • 1
  • 23
  • 27
  • You say "make sure onActivityResult is "protected" when you're overriding it" could you clarify? Did you just mean that it should not be declared private? – Sojurn Dec 30 '13 at 11:06
1

You can use FLAG_ACTIVITY_REORDER_TO_FRONT, which will bring the launched activity to the front of it's task's stack instead of creating a new instance.

Note that the result will be delivered to the first activity that called startActivityForResult. This answer provides a good example of how this works.

Community
  • 1
  • 1
Jomel Imperio
  • 924
  • 4
  • 16