首页 > 代码库 > android onResultActivity不执行原因总结

android onResultActivity不执行原因总结

 插一个注意点.在用ArrayAdapter的时候数据那里的类型必须是List<String> 不能是ArrayList<String>

1.在A.Activity中调用startActivityForResult()启动B.Activity后,再B.Activity中调用setResult() 方法后,A.Activity中的onResultActivity将会执行。

但遇到下列问题就不会执行了:

  startActivityForResult()用法参照:http://www.cnblogs.com/liangstudyhome/p/3708296.html

  •   Activity的启动方式是singleTask 或者是singleMode
  •   在传递的Intent中requescode小于0
  •      *** 就是在调startActivityForResult()的Activity在一个GroupActivity中( 其实是执行了GroupActivity的onResultActivity方法)

2.对上述问题的解决办法:

  •   将Activity的启动方式改成stander

1. tab activity(想获取onResultActivity值的子Activity)中使用

  

1 getParent().startActivityForResult(intent,REQUEST);  


2. 当子activity.finish 返回时,在ActivityGroup实现onActivityResult,内容类似于:

  

1     @Override  2         protected void onActivityResult(int requestCode, int resultCode, Intent data) {  3             // 向下传递  4         YourActivity currentActivity = (YourActivity) getLocalActivityManager()  5                 .getCurrentActivity();  6         currentActivity.onChildActivityResult(requestCode, resultCode, data);  7         }  

onChildActivityResult是一个你自己实现的方法,用来处理返回结果的。

具体的一个例子:

  

 1 @SuppressWarnings("deprecation") 2     private void tabChange(CompoundButton buttonView, boolean isChecked) { 3         if (isChecked) { 4             containerBordy.removeAllViews(); 5             Intent intent = null; 6             switch (buttonView.getId()) { 7             case R.id.tab_weather: 8  9                 intent = new Intent(MainActivityTab.this,10                         WeatherActivityBaiDu.class);11                 break;12             case R.id.tab_produce:13 14                 intent = new Intent(MainActivityTab.this,15                         ProductWarningActivity.class);16                 break;17             case R.id.tab_im:18                 intent = new Intent(MainActivityTab.this,19                         ChatBriefActivity.class);20                 break;21 22             case R.id.tab_diaster:23                 intent = new Intent(MainActivityTab.this,24                         RelevantForecastActivity.class);25                 break;26             case R.id.tab_setting:27                 intent = new Intent(MainActivityTab.this, SettingActivity.class);28                 break;29             case R.id.tab_more:30 31                 break;32 33             }34 35             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);36 37             Window subActivity = mlam.startActivity("startActivity", intent);38             containerBordy.addView(subActivity.getDecorView());39 40         }41 42     }

上面的方法是将一个Activity加载到一个GroupActivity中。

要注意的是这的Activity是有一个startActivity的id,所以要将上述的onActivityResult方法变成:

 1 @Override 2     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 3         super.onActivityResult(requestCode, resultCode, data); 4         System.out.println("返回的是这里的onActivityResult"); 5         CommunicationActivity currentActivity = (CommunicationActivity) mlam 6                 .getActivity("startActivity"); 7          8 //        Activity a = mlam.getCurrentActivity() 9         System.out.println("currentActivity-->>" + currentActivity);10         11         12 //        System.out.println("requestCode-->>" + requestCode13 //                + "    resultCode-->>" + resultCode + "  data"14 //                + data.getIntExtra("blockID", 0));15         currentActivity.onActivityResultFromMainActivity(requestCode,16                 resultCode, data);17 //        currentActivity.YSOS();18     }
mlam对应的是LocalActivityManager.
onActivityResultFromMainActivity
这是子Activity中自己定义的方法。

 

android onResultActivity不执行原因总结