I have a fragment which adds an option to the option menu. When this option is clicked a dialog fragment opens. The dialog has the original fragment set to its target fragment. If no orientation change happens while the dialog fragment is open the target fragment is as expected but after orientation change the target fragment is set to the dialog fragment itself instead of the fragment which was set previously. As a result I get a classCastException when trying to cast the target fragment to the fragment which was set as the target fragment. I need to get the target fragment in my dialog because it implements a callback (OnStartOrRestartLoader). I've been trying to solve this issue for over a week and would really appreciate if somebody could point me in the right direction. Since this is my first question here I apologize if I've excluded some necessary information or if this is not an appropriate question here.
In the fragment (VirsārstsFragment) which implements OnStartOrRestartLoader I create the dialog as follows:
FragmentManager fm = getChildFragmentManager();
SearchDialogFragment dialog = new SearchDialogFragment();
dialog.show(fm, "searchDialog");
dialog.setTargetFragment(this, DIALOGFRAGMENT);
Then in the 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
getTargetFragment()
returns aFragment
and you are trying to cast it toOnStartOrRestartLoader
. The exception is very clear. What are you trying to do? – Riser