简体   繁体   中英

android : How to work around the fact that a singleInstance activity cannot expect to rely onActivityResult when starting subActivity?

I am making an android application, which has the following execution flow:

  1. A service registers a PendingIntent with the AlarmManager

  2. When the alarm is launched, a Receiver receives the intent, and (given some conditions) calls startsActivity() for my Main Activity , which in the manifest has been declared as android:launchMode="singleInstance" . Note that for this call to work, the intent passed should have an Intent.FLAG_ACTIVITY_NEW_TASK

  3. When started, Main Activity modifies itself a bit, and calls startActivityForResult for an Activity, which we'll call WebviewActivity (because it contains a webview, but that's besides the point)

  4. When the user is done interacting with the WebViewActivity , setResult() and finish() are called on it, and one would expect for MainActivity.onActivityResult() to be called.

But of course this does not happen, as has been documented in many discussions here, the reason apparently being that an Activity launched from a singleInstance Activity, runs in a different Task.

A solution I think would be to have the WebActivity start the MainActivity instead.

The question is, is there a way to maintain onActivityResult being called at the right time? In that case, which aspects from the starting point of the execution flow should change?

Please note that MainActivity should not have multiple instances at the same time (it is basically an interface to the service) but if its launchMode is set to standard , the Receiver, because of the FLAG_ACTIVITY_NEW_TASK that is required, will do just that.

Manifest declaration of MainActivity :

    <activity   android:name=".activities.MainActivity"
                android:label="@string/app_name" 
                android:launchMode="singleInstance"
                android:configChanges="keyboardHidden|orientation|screenSize">

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

        android:uiOptions=”splitActionBarWhenNarrow”
    </activity>

Receiver launches MainActivity by calling

 onReceive(Context context, Intent intent) 
 {
     intent.setClass(context, MainActivity.class);
     int flag = Intent.FLAG_ACTIVITY_NEW_TASK;
     intent.setFlags(flag);
     context.startActivity(intent);
 }

I use the following workaround for this problem:

Activity A is the caller

Activity B is the singleInstance activity from which I want the result

In activity AI register a broadcast receiver as following

PickReceiver receiver=new PickReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("ActivityA_pick");
registerReceiver(receiver,filter);

class PickReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(intent.getAction().equals("ActivityA_pick")){
        //get data from intent extras
    }
}

In ActivityB when it's time to send the data I use:

sendBroadcast("ActivityA_pick").putExtra("...data...");
finish();

This way i can get the result I want when I want a result from one of my own activities. If you want a result from the system or another app you can adjust this using a dummy activity that doesn't have launch mode singleInstance, have it start the activity for result and when it gets it onActivityResult it sends the broadcast to the caller.

Hope this helps

As the Main Activity is a single instance, is doing what it has been told.

So yes, you have to start the Main Activity from the Web Activity in order to be coherent with the tasks executions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM