startActivityForResult之后无法回调onActivityResult方法(ActivityGroup另解)

    现在如果在百度找“startActivityForResult之后无法回调onActivityResult方法”得到的回答要么就是requestCode没有大于0,要么就是目标activity在invoke  setResult方法以后没有finish,要么就是activity的launchMode问题,但是这里要讲的,是一种特例,下面开始:

I had an ActivityGroup managing sub-activities. One of the sub-activities called a similar external intent (external to my app). It never called the onActivityResult within the sub-activity that started it.

I finally figured out/remembered that the issue is because Android will only allow a nested layer of sub-activities...ie sub-activities can't nest sub-activitites. To solve this:

  1. call getParent().startActivityForResult() from your sub-activity
  2. your parent (the activitygroup) will be able to handle the onActivityResult. So I created a subclass of ActivityGroup and handled this onActivityResult.
  3. You can re-route that result back to the sub-activity if you need to. Just get the current activity by getLocalActivityManager().getCurrentActivity() . My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data) in that subclass for the ActivityGroup to call.

当你有一个ActivityGroup管理了好几个activity时,如果其中一个activity调用startActivityForResult方法,那么,就永远无法回调到这个Activity的onActivityResult 方法,主要问题就出在(我靠,四级426的伤不起啊,勉强看吧)android只允许单层嵌套的activity去start然后回调,而activityGroup内部的activity是不可以的(如果有指正的,一定提出来啊,以上我是瞎编的),解决办法就是:

在startActivityForResult的前面加上getParent(),这样的话,activityGroup就会收到onActivityResult(),如果你需要在子activity中处理,可以重定向过去,通过在activityGroup中调用getLocalActivityManager().getCurrentActivity() 获取当前活动的activity,当然,在获得到这个activity以后是不能activity.onActivityResult()这样调用的,我这边是因为所有的activity都继承了一个暂且叫做CommonActivity吧,在

CommonActivity中写一个方法handleActivityResult( int requestCode, int resultCode, Intent data){onActivityResult(…………)}然后在ActivityGroup中调用这个方法就可以了,当然ActivityGroup也是继承了CommonActivity,简单的回调。

好了,到此结束。收工放假!以上翻译我心里有数,很多地方可能翻译不到位,但解决方法我试过是可以的,如果对翻译有看法,可以留言,大家共同进步!谢谢!

你可能感兴趣的:(startActivityForResult之后无法回调onActivityResult方法(ActivityGroup另解))