The solution that onActivityResult is not called in Android Fragment

onActivityResult in Fragment is not called. There are several situations, please check one by one.

 

1. Check whether the onActivityResult method is overridden in the Activity to which the Fragment belongs.

If this is the case, make sure that super.onActivityResult is written:

  1. @Override  
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.     super.onActivityResult(requestCode, resultCode, data);  
  4.     if (resultCode == RESULT_OK) {  
  5.         //Related processing code in Activity  
  6.     }  
  7. }  

 

2. Check the call mode of startActivityForResult in Fragment.

Make sure you don't use the getActivity().startActivityForResult method (you need to override the onActivityResult method in the Activity that the Fragment depends on at this time), but use the startActivityForResult directly.

 

3. If neither of the above two aspects can solve the problem, it is likely that the situation is slightly complicated as follows:

In this case, nested fragments are used in the support library, that is to say, nested fragments are used, such as displaying fragments with ViewPager. This can be regarded as a Bug of Google. For details, please refer to this blog:

http://blog.shamanland.com/2014/01/nested-fragments-for-result.html

Solution:

The best way is to write a BaseActivity to inherit FragmentActivity and override the onActivityResult method. All activities you use inherit BaseActivity. The specific implementation of BaseActivity:

 

  1. package com.jupaidaren.android;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Intent;  
  6. import android.support.v4.app.Fragment;  
  7. import android.support.v4.app.FragmentActivity;  
  8. import android.support.v4.app.FragmentManager;  
  9. import android.util.Log;  
  10.   
  11. abstract public class BaseActivity extends FragmentActivity {  
  12.   
  13.     private static final String TAG = "BaseActivity";  
  14.   
  15.     @Override  
  16.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  17.         FragmentManager fm = getSupportFragmentManager();  
  18.         int index = requestCode >> 16;  
  19.         if (index != 0) {  
  20.             index--;  
  21.             if (fm.getFragments() == null || index < 0  
  22.                     || index >= fm.getFragments().size()) {  
  23.                 Log.w(TAG, "Activity result fragment index out of range: 0x"  
  24.                         + Integer.toHexString(requestCode));  
  25.                 return;  
  26.             }  
  27.             Fragment frag = fm.getFragments().get(index);  
  28.             if (frag == null) {  
  29.                 Log.w(TAG, "Activity result no fragment exists for index: 0x"  
  30.                         + Integer.toHexString(requestCode));  
  31.             } else {  
  32.                 handleResult(frag, requestCode, resultCode, data);  
  33.             }  
  34.             return;  
  35.         }  
  36.   
  37.     }  
  38.   
  39.     /** 
  40.      * Recursive call, effective for all child fragments 
  41.      *  
  42.      * @param frag 
  43.      * @param requestCode 
  44.      * @param resultCode 
  45.      * @param data 
  46.      */  
  47.     private void handleResult(Fragment frag, int requestCode, int resultCode,  
  48.             Intent data) {  
  49.         frag.onActivityResult(requestCode & 0xffff, resultCode, data);  
  50.         List<Fragment> frags = frag.getChildFragmentManager().getFragments();  
  51.         if (frags != null) {  
  52.             for (Fragment f : frags) {  
  53.                 if (f != null)  
  54.                     handleResult(f, requestCode, resultCode, data);  
  55.             }  
  56.         }  
  57.     }  
  58. }  

Tags: Fragment Android Google Java

Posted on Mon, 04 May 2020 03:21:47 -0400 by moberemk