startActivityForResult from ActivityGroup?

孤者浪人 提交于 2019-12-28 02:07:44

问题


I can't seem to get ANY result when trying to start an activity from an activitygroup. I've put an onactivityresult in the activity and activitygroup? Specifically I'm trying to let the user choose a photo/video from the Intent.ACTION_GET_CONTENT, but I never get anything back? What am I doing wrong?

Here is how I call the code:

Intent pickMedia = new Intent(Intent.ACTION_GET_CONTENT);
   pickMedia.setType("video/*");
   startActivityForResult(pickMedia,12345);

Any ideas?


回答1:


I've had a similar issue. 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.



回答2:


In Your Parent Activity

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if (requestCode == YOUR_REQUEST_CODE)   {

        CHILD_ACTIVITY_NAME activity = (CHILD_ACTIVITY_NAME)getLocalActivityManager().getCurrentActivity();

        activity.onActivityResult(requestCode, resultCode, intent);}}

So your childern activity's onActivityResult will run.



来源:https://stackoverflow.com/questions/4268178/startactivityforresult-from-activitygroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!