Android fragment nested fragment solution, androidfragment
Fragment is easy to use, duang ~~, If you have encountered another problem, record it and share it with those who have encountered the problem!
1. When fragment is nested, getActivity () is null.
Activity A nested fragment B, B nested fragment C, C jump to activity D, when activity D is completed, it is easy to find that getActivity is empty in C. if your activity is recycled, you need to save the fragment information in the bundle. My solution: After the fragment is instantiated, it will be registered in the fragmentManager of the activity, this action is encapsulated in the onAttach (Activity activity) method in fragment. You can assign values directly in the onAttach (Activity activity) method.
2. When fragment is nested, The onActivityResult method of the sub-fragment is not executed.
Activity A is nested with fragment B and B is nested with fragment C. C jumps to activity D through startActivityForResult. After activity D is finished, onActivityResult of C is not executed.
In fact, not everyone will encounter this problem. When activity A and Fragment B do not rewrite the onActivityResult method, this problem may not occur, but activity A has rewritten the onActivityResult method, therefore, when activity D is destroyed, the onActivityResult of the activity is directly executed. In fact, this problem is also very simple, but it may not be imagined when it is encountered, the solution is to add super to the onActivityResult method of Activity. onActivityResult (requestCode, resultCode, data); everything is OK
OnActivityResult method of Activity:
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);...}
The onActivityResult method of fragment:
public void onActivityResult(int requestCode, int resultCode, Intent data) {}
Of course, the onActivityResult method is not used in some special cases. Do not get bored when you encounter it. Manually call the fragment method from the onActivityResult method in the Activity and pass the corresponding data to it.
If you have a better way to communicate with each other!