fragment nested fragment problem solutions in Android

  • 2020-06-23 01:51:34
  • OfStack

fragment easy to use, duang~~, again encountered a problem, record 1, share this problem to the students!
getActivity() is null when fragment is nested within fragment

activity A nested fragment B,B nested fragment C,C jumped to activity D when activity If your activity is recycled, you need to save 1 fragment message in bundle. My solution: After instantiating fragment, you will register 1 onAttach(Activity activity) in fragment, which can be assigned directly in onAttach(Activity activity)

2.fragment nested fragment without executing the onActivityResult method of the child fragment
activity A nested fragment B,B nested fragment C,C jumped to activity D via startActivityForResult, when activity D was dropped by finish, onActivityResult of C was not executed
Not everyone has this problem. If your activity A and Fragment B don't override onActivityResult, it probably won't happen. But my activity A overwrote onActivityResult, so when activity D is destroyed, go ahead and execute activity In fact, this problem is also very simple, but when you encounter it, you may not think of the solution is to add super.onActivityResult(requestCode, resultCode, data) to the onActivityResult method. It's all ok
Method of onActivityResult:


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data); 
 ...
}

onActivityResult method:


public void onActivityResult(int requestCode, int resultCode, Intent data) {
 
}

Of course, there are some special cases do not go onActivityResult method, encountered do not fret, from Activity onActivityResult method in the manual call fragment method, the corresponding data passed
If you have a better way, communicate with each other!


Related articles: