2

I have an app where multiple emergency messages can be received. When each message is received it needs to open a dialog with the emergency details. If more than one emergency is received it will stack the dialogs so that when the most recent emergency message is dealt with and that particular dialog closes, the previous most recent and un-handled emergency should appear, as if they are stacked.

Also, the details for each emergency are updated every x seconds from a service and these changes need to make their way to the correct dialog instance so that, when on screen the dialog is up to date.

I had this working in, I think, an un-efficient way. I was storing the instance of each dialog fragment object in a list and updating that instance with new details and then opening it. This meant that if 50 emergencies were spammed I was storing 50 dialogs. Not great for memory.

Also, this method didn't work well with orientation, where the object is destroyed and re-built, it was taken out of my list and the details reverted to the original details which were stored in it's intent.

I am looking for a way to do this that is as efficient as possible and I wanted to ask your collective Android brain for suggestions.

How would I efficiently manage multiple instances of the same dialog class to get the behaviour I require?

Stephen
  • 762
  • 1
  • 12
  • 32

1 Answers1

1

What i would do, is to just hold EmergencyMessage data object stack in my Activity. When stack is not empty, i would show new DialogFragment, and pass it EmergencyMessage from top of the stack, using Fragment.setArguments.

In my DialogFragment, i would use Fragment.getArguments to get EmergencyMessage object and display message. When user deals with message, notify your activity (for example like this) and close dialog. Then, if you still have unhandled EmergencyMessages in your stack, just repeat the process.

This way you always have single DialogFragment instance active at any given time.

Now, regarding updating the EmergencyMessage contents if DialogFragment is already showing this message, you can do it from activity like this:

When you create dialog, pass it tag "EmergencyMessageDialog" so you can retreive dialog later and interact with it.

MyDialogFragment dialog = (MyDialogFragment)
    getFragmentManager().findFragmentByTag("EmergencyMessageDialog");
dialog.updateEmergencyMessage(newEmergencyMessage);

If you receive update for EmergencyMessage that hasnt been shown yet, you can just update your EmergencyMessage object in your stack in activity, so that would be easy.

Community
  • 1
  • 1
hendrix
  • 3,364
  • 8
  • 31
  • 46
  • Hi there,Many thanks for the response. This is almost the same as I have it at the moment. But this doesn't give the effect of having a stack of emergencies. In that if the top one of two is closed, the second should then be open in the UI. I am investigating using a grid/listview to better handle these multiples. – Stephen Nov 22 '13 at 09:47
  • @Stephen I am not sure which 'stack' effect are you talking about, but you should be able to specify animation for show/hide dialog. See http://stackoverflow.com/questions/13402782/show-dialogfragment-with-animation-growing-from-a-point – hendrix Nov 22 '13 at 09:50
  • Hi there,Thanks for the update. I believe this would definitely work – Stephen Nov 22 '13 at 09:58