Android - after changing the orientation of the target fragment in the dialogue fragment

I have a snippet that adds an option to the options menu. Clicking this option opens a fragment of the dialog. A dialog has a source fragment set to its target fragment. If the orientation change does not occur when the dialog fragment is opened, the target fragment will be as expected, but after the orientation change, the target fragment is installed on the dialogue fragment itself, and not on the fragment that was installed earlier. As a result, I get a classCastException when trying to pass the target fragment to the fragment that was set as the target fragment. I need to get the target fragment in my dialog box because it implements a callback (OnStartOrRestartLoader). I have been trying to solve this problem for more than a week and would really appreciate it if someone could point me in the right direction. Since this is my first question, I apologize if I exclude any necessary information or if there is no corresponding question.

In the fragment (VirsārstsFragment) that implements OnStartOrRestartLoader, I create a dialog as follows:

FragmentManager fm = getChildFragmentManager(); SearchDialogFragment dialog = new SearchDialogFragment(); dialog.show(fm, "searchDialog"); dialog.setTargetFragment(this, DIALOGFRAGMENT); 

Then in SearchDialogFragment I do the following to set the callback:

 OnStartOrRestartLoader callback = (OnStartOrRestartLoader) getTargetFragment(); 

Logcat:

 08-25 12:15:55.087: E/AndroidRuntime(10057): FATAL EXCEPTION: main 08-25 12:15:55.087: E/AndroidRuntime(10057): Process: com.example, PID: 10057 08-25 12:15:55.087: E/AndroidRuntime(10057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.ClassCastException: com.example.SearchDialogFragment cannot be cast to com.example.OnStartOrRestartLoader 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3759) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.ActivityThread.access$900(ActivityThread.java:141) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.os.Handler.dispatchMessage(Handler.java:102) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.os.Looper.loop(Looper.java:136) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.ActivityThread.main(ActivityThread.java:5052) 08-25 12:15:55.087: E/AndroidRuntime(10057): at java.lang.reflect.Method.invokeNative(Native Method) 08-25 12:15:55.087: E/AndroidRuntime(10057): at java.lang.reflect.Method.invoke(Method.java:515) 08-25 12:15:55.087: E/AndroidRuntime(10057): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 08-25 12:15:55.087: E/AndroidRuntime(10057): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 08-25 12:15:55.087: E/AndroidRuntime(10057): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 08-25 12:15:55.087: E/AndroidRuntime(10057): at dalvik.system.NativeStart.main(Native Method) 08-25 12:15:55.087: E/AndroidRuntime(10057): Caused by: java.lang.ClassCastException: com.example.SearchDialogFragment cannot be cast to com.example.OnStartOrRestartLoader 08-25 12:15:55.087: E/AndroidRuntime(10057): at com.example.SearchDialogFragment.onCreateDialog(SearchDialogFragment.java:59) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:307) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1901) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1518) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:962) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1901) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:567) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.Activity.performStart(Activity.java:5322) 08-25 12:15:55.087: E/AndroidRuntime(10057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187) 08-25 12:15:55.087: E/AndroidRuntime(10057): ... 13 more 
+5
source share
3 answers

setTargetFragment should not be used to establish a relationship between parent / child fragments. It should only be used to link fragments of a sister (i.e., those that exist within the same FragmentManager ).

Due to the fact that it does not work between parent / child fragments, the fragment target is saved and restored as an index into its own FragmentManager . Thus, everything will be awkward until the structure restores activity after, for example, a change in orientation. At this point, the FragmentManager will look for the target fragment within itself, and not the parent FragmentManager .

+3
source

I still don’t understand why getTargetFragment didn’t work for me, but replacing it with getParentFragment solved my problem.

+2
source

If there is no user interface change, you can add the configChanges property to the action in the manifest

 <activity android:name=".YourActivity" android:configChanges="screenSize|orientation"/> 

What does this mean, it indicates that a configuration change when changing screens or orientation occurs in the activity itself (in your case there are no changes, so nothing needs to be done)

If you will need to handle any changes in the future, you can do this in onConfigurationChanged () in your activity.

Let me know if this works in your case.

-3
source

Source: https://habr.com/ru/post/1201061/


All Articles