[英]Android: onActivityResult() is not calling for startActivityForResult
[英]Android: startActivityForResult not calling onActivityResult
使用Log.i我已經確認步驟1到3發生了。 但是,當應該調用onActivityResult()方法時,我在日志中得到以下內容:
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) }
每個實體都在一個單獨的項目中(我有3個項目相互交互),因此它們都在自己的流程中運行。
MainActivity從服務啟動,代碼如下:
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);
返回結果活動:
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;
}
}
}
主要活動
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
}
}
我的LogCat窗口中沒有顯示堆棧跟蹤。 請幫忙!
編輯:有趣的新信息。 如果我然后從啟動器啟動MainActivity,我在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
我的理論是,沒有運行包含MainActivity的任務,因為消息是從CentralService接收的。 有什么想法嗎? 知道如何切換到正確的任務。 (注意即使這可能看起來很糟糕,也可能會破壞用戶切換任務並將另一個Activity帶到最前端,這就是我想要做的。這是因為這最終將在自定義版本的android上運行,這將使所有這不是破壞性的。)
2年后回到這個問題后,問題是因為活動是在一項新任務中啟動的。 (我把它設置在我開火的意圖中)。 為立即啟動新任務的結果啟動活動將返回-1。
打電話后
startActivityForResult(listenIntent, 500);
在com.custom.returnresultaction類中,您是否包含此代碼?
setResult(RESULT_CANCELED, i);
or setResult(RESULT_OK, i);
and call finish();
您需要設置結果並調用finish()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.