All said fragment easy to use, duang~~, and encountered problems, record, share to meet this problem classmate!
1.fragment getactivity () is null when nesting fragment
Activity a nested fragment b,b nested fragment c,c jump to activity D, when activity D is finished, C is prone to burst getactivity empty. If your activity is recycled, Then you need to save the fragment information in the bundle, my workaround: Fragment will be registered in the activity Fragmentmanager after instantiation, this action is encapsulated in fragment Onattach ( Activity) method, you can assign a value directly to the Onattach (activity activity) method
2.fragment nested fragment when there is no Onactivityresult method to perform sub-fragment
activity a nested fragment b,b nested fragment c,c jump to activity D via Startactivityforresult, and when activity D is finished, Did not execute C's Onactivityresult
In fact this problem is not everyone will encounter, when your activity A and fragment B do not rewrite the Onactivityresult method, may not appear this problem, but my activity a rewrite the Onactivityresult method, So when activity D is destroyed, go directly to the activity of the onactivityresult, in fact, the problem is very simple, but when encountered may not be able to imagine, The solution is to add Super.onactivityresult (Requestcode, ResultCode, data) to the Onactivityresult method in the activity; everything's OK.
Onactivityresult Method of activity:
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data), ...}
Fragment Method of Onactivityresult:
public void Onactivityresult (int requestcode, int resultcode, Intent data) {}
Of course, there are special circumstances do not go Onactivityresult method, encountered also do not fret, from the activity of the Onactivityresult method to manually call fragment method, the corresponding data passed through
If you have a better way to communicate with each other Oh!
Android fragment nested fragment problem solving solution