我有兩個獨立的應用程序。應用程序A和應用程序B.我想從應用程序A開始應用程序B中的活動並獲得結果。在應用程序B中還有一個活動。從B第二項活動中,我得到了B的第一項活動。現在我想將這些結果返回給應用程序A.但是,A中的OnActivityResult永遠不會被調用。以下是代碼。OnActivityResult永遠不會被調用
應用程序A:
public void onClickBtnToApplicationB(View v) {
try {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
final ComponentName cn = new ComponentName("pakacagename","package.class");
intent.setComponent(cn);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
//handle Exception
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case REQUEST_CODE:
handleResult(resultCode, intent);
break;
}
}
public void handleResult(int resultCode, Intent intentResult) {
switch (resultCode) {
case RESULT_OK:
String Result = intentResult.getStringExtra("RESULT");
// I need Results from Application B here..
break;
case RESULT_CANCELED:
break;
}
}
應用B活動1.class:
Intent s = new Intent(1.this,2.class);
startActivityForResult(s, REQUEST_CODE_B);
protected void onActivityResult(int requestCode, int resultCode, Intent intentResult) {
switch(requestCode){
case REQUEST_CODE_B:
handleResult(resultCode, intentResult);
}
}
public void handleResult(int resultCode, Intent intentResult) {
switch (resultCode) {
case RESULT_OK:
String scanResult = intentResult.getStringExtra("RESULT");
Intent newintent = new Intent();
newintent.putExtra("RESULT", scanResult);
setResult(Activity.RESULT_OK, newintent);
finish();
break;
case RESULT_CANCELED:
break;
}
應用程序B是否也是您的應用程序? –
@Hoan Nguyen Yup – Shah