如何在轮换后获取DialogFragment的处理程序

时间:2017-02-27 07:08:57

标签: android android-dialogfragment

我有一个DialogFragment附加到一个活动,当屏幕旋转时,dialogFragment生命周期将由Android框架自动激活,我的问题是如何在旋转后获取dialogFragment的处理程序对象因为我想要在我需要的代码中将它dimiss。前处理程序对象为null,因为在旋转时会调用活动。

我尝试将setRetainInstance(true)放入DialogFragment中的onCreate函数中,但是当我旋转屏幕时遇到崩溃:

FATAL EXCEPTION: main
Process: com.microsoft.pdfviewertestapplication, PID: 6075
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.microsoft.pdfviewertestapplication/com.microsoft.pdfviewertestapplication.MainActivity}: java.lang.IllegalStateException: DialogFragment can not be attached to a container view
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
    at android.app.ActivityThread.-wrap15(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5422)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: DialogFragment can not be attached to a container view
    at android.app.DialogFragment.onActivityCreated(DialogFragment.java:476)
    at android.app.Fragment.performActivityCreated(Fragment.java:2228)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:992)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
    at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1953)
    at android.app.FragmentController.dispatchActivityCreated(FragmentController.java:152)
    at android.app.Activity.performCreateCommon(Activity.java:6245)
    at android.app.Activity.performCreate(Activity.java:6253)
    .....

任何帮助表示赞赏!谢谢。

0 个答案:

没有答案