Tuesday, January 19, 2016

Quick Summary: List of things to do for DialogFragment with State

These are my notes for creating a DialogFragment in Android.  There are a number of book-keeping details.  I'll post some code when I get this app I'm building finished.

Things that need to be done:
  • onAttach
    • check for:
      • Pre-M (23) versions and provide method taking Activity
      • M and beyond versions, providing method taking Context
  • onDetach
  • saveInstanceState
    • save settings/state into the bundle passed into the method.
  • onDestroy
    • save settings/state to persistant state (prefs)
  • onCreateDialog
    • Check for savedInstanceState
    • Check for getArguments - some dialogs pass arguments on load
    • If neither savedInstanceState or getArguments, check persistent state (prefs, db).
    • populate multiple choice items, single choice, adapters...
  • if using onCreateView
    • configure controls, set defaults
    • configure click listeners, checkbox listeners (if not in adapter)
      • CheckBox listeners that are aligned with a layout in a compound configuration (Layout holding TextView and CheckBox) need to call setOnCheckedChangeListener(null) before setting the checkbox from the click in the layout, then set it back to setOnCheckedChangeListener(this), in order to prevent a double-click.
  • on item click
    • update whatever member variables that need updating, so they get saved in any configuration or life-cycle change.
  • on positive button  click
    • pass data back to the listener configured in onAttach
  • on negative button click
    • Often do nothing, but sometimes pass a message back to notify whoever called the dialog that it needs to restore the state it had set when it called the previous dialog.
I'll add details when/if I notice anything's missing, but I'll probably add more detail to this article.

No comments:

Post a Comment