My Setup
- A Service running in its own process, CentralService
- An activity that calls startActivityForResult(), MainActivity
- The activity that is being started for result, ReturnResultActivity
What I'm trying to do
- Start ReturnResultActivity and have it bind to the service (register its handler)
- Let whatever other activities want to run run
- When it receives a message from the service:
- Unbind from the Service
- finish()
- setResult()
- Have the MainActivity's onActivityResult() method called
Using Log.i I have confirmed that steps 1 to 3 happen. However when the onActivityResult() method should be called I get the following in the log:
V/ActivityManager( 60): Finishing activity: token=HistoryRecord{442cc518 com.myActivity/.primitives.ReturnResultActivity}, result=3, data=Intent { (has extras) }
V/ActivityManager( 60): Adding result to HistoryRecord{442a3988 com.mainActivity.sample/.MainActivity} who=null req=500 res=3 data=Intent { (has extras) }
Additional information
Every entity is in a separate project (I have 3 projects interacting with each other) and therefore they are all running in their own process.
The MainActivity is started from the Service with the following code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(activityInfo.packageName, activityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
Some of the Code
Return Result Activity:
public class ReturnResultActivity extends SomeActivity {
private static final boolean DEBUG = true;
private static final String TAG = "ReturnResultActivity";
protected void onBind() {
// We want to monitor the service for as long as we are
// connected to it.
Message msg = Message.obtain(null,
CentralService.MSG_LISTEN);
msg.replyTo = mMessenger;
try {
mService.send(msg);
} catch (RemoteException e) {
// In this case the service has crashed before we could even
// do anything with it; we can count on soon being
// disconnected (and then reconnected if it can be restarted)
// so there is no need to do anything here.
e.printStackTrace();
}
}
/** this method is called eventually after doUnbindService is called **/
protected void onUnbind() {
if (DEBUG) Log.i(TAG, "Unbinding and finishing"); //I can tell this gets called when I run it
if (data != null) {
setResult(CentralService.MSG_SPEECH_RECOGNIZED, data);
}
finish();
}
Intent data;
protected boolean receiveMessage(Message msg) {
if (DEBUG) Log.i(TAG, "Incoming Message to Listener: " + msg.what);
switch (msg.what) {
case ASRManager.MSG_SPEECH_RECOGNIZED:
data = new Intent();
Bundle bundle = msg.getData();
bundle.setClassLoader(getClassLoader());
data.putExtra(ASRManager.TOKEN_PARCEL_KEY, bundle.getParcelable(ASRManager.TOKEN_PARCEL_KEY));
if (DEBUG) Log.i(TAG, "Received speech, setting result.");
doUnbindService();
return true;
default:
return false;
}
}
}
Main Activity
public class MainActivity extends CustomActivity {
private static final boolean DEBUG = true;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent("com.custom.returnresultaction");
startActivityForResult(listenIntent, 500);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (DEBUG) Log.i(TAG, "Got activity result " + resultCode); //this doesn't get called
}
}
No stack traces show up in my LogCat window. Please help!
EDIT: Interesting new information. If I then launch MainActivity from the launcher I see the following in logcat:
I/ActivityManager( 60): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mainActivity.sample/.MainActivity }
V/ActivityManager( 60): Sending result to HistoryRecord{44253568 com.android.launcher/com.android.launcher2.Launcher} (index -1)
I/ActivityManager( 60): Starting Activity Unchecked Locked
V/ActivityManager( 60): com.mainActivity.sample.MainActivity Audio Activity Found
V/ActivityManager( 60): Delivering results to HistoryRecord{442c0310 com.mainActivity.sample/.MainActivity}: [ResultInfo{who=null, request=500, result=3, data=Intent { (has extras) }}]
V/ActivityThread( 327): Handling send result to ActivityRecord{440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample/com.mainAcitivty.sample.MainActivity}}
V/ActivityThread( 327): Delivering result to activity ActivityRecord{440de270 token=android.os.BinderProxy@440ddca0 {com.mainActivity.sample/com.mainActivity.sample.MainActivity}} : ResultInfo{who=null, request=500, result=3, data=Intent { (has extras) }}
I/MainActivity( 327): Got activity result 3
My theory is that the task is not running which contains the MainActivity because the message is received from the CentralService. Any thoughts? Any idea how to switch to the correct task. (note even though this may seem bad practice and disruptive to switch tasks on a user and bring another Activity to the forefront this is what I want to do. This is because this will eventually be running on a custom version of android which will make all this not disruptive.)
After you call
In the com.custom.returnresultaction class, are you including this code?
You need to set the result and call finish().
After coming back to this question 2 years later the issue is because the activity is launched in a new task. (I set this in the intent I fire). Starting an activity for result that launches in a new task immediately will return -1.