Sunday, April 14, 2013

[android-developers] When to show a dialog from Fragment and when not to

I have a Fragment that fires an async task to connect to a remote server. If it fails to connect (eg times out) then I use use a Handler to display a dialog to the user.

final DialogFragment newFragment = AlertDialogFragment.createConnectionFailedDialog(getSupportActivity());
newFragment.show(getFragmentManager());

All good except that the async task if the user navigates away from the Activity hosting that Fragment (eg goes to Preferences) then when the time out occurs the Fragment is in a quasi shut down state and the following exception is thrown.

04-14 23:11:51.407: ERROR/AndroidRuntime(6267): FATAL EXCEPTION: main
        java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
        at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
        at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
        at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
        at org.holoeverywhere.app.DialogFragment.show(DialogFragment.java:369)
        at org.holoeverywhere.app.DialogFragment.show(DialogFragment.java:350)
        at org.holoeverywhere.app.DialogFragment.show(DialogFragment.java:329)
        at au.com.xandar.wirelesstiming.connection.ConnectionFragment.updateUserInterfaceOnClose(ConnectionFragment.java:197)
        at au.com.xandar.wirelesstiming.connection.ConnectionFragment.access$200(ConnectionFragment.java:26)
        at au.com.xandar.wirelesstiming.connection.ConnectionFragment$2.run(ConnectionFragment.java:96)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

What is the correct way to handle this scenario?

Ideally I'd like to always display the dialog, but if that is not an option, then what do I use to determine whether the dialog should be displayed or not?


William 

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment