Leo
Leo

Reputation: 4681

Prevent multiple instances of Activity but still use onActivityResult

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.

Upvotes: 2

Views: 3010

Answers (2)

Jomel Imperio
Jomel Imperio

Reputation: 934

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.

Upvotes: 1

serkanozel
serkanozel

Reputation: 2927

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

Upvotes: 3

Related Questions