Refresh List Dialog in Android with removeDialog() in onPrepareDialog method
Is it considered bad form to call removeDialog(int) in onPrepareDialog as shown below to force re-creating the dialog every time it is called with showDialog():
@Override
public void onPrepareDialog(int id, Dialog dialog)
{
switch(id)
{
case REMOVE_WATCH_WORD :
removeDialog(REMOVE_WATCH_WORD);
break;
}
}
and in the onCreateDialog I handle it with this case argument:
case REMOVE_WATCH_WORD :
CustomHelper ch = new CustomHelper();
final CharSequence[] watchWordsCharSequence = ch.getWatchWordsAsCharSequence(getApplicationContext());
return new AlertDialog.Builder(ActivityName.this)
.setTitle("Remove Word from List")
.setItems(watchWordsCharSequence, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), "\"" + watchWordsCharSequence[item] + "\" removed from List!", Toast.LENGTH_SHORT).show();
String removeString = (String) watchWordsCharSequence[item];
SharedPreferences.Editor editor = watchWords.edit();
editor.remove(removeString);
editor.commit();
}
}).create();
Is it considered bad form to force the dialog to be re-drawn every time it is called? What ar开发者_StackOverflowe the benefits/disadvantages in doing so?
Yes, I would consider that bad form. Does it even work? onPrepareDialog()
is called even after the first time you create the dialog, which means you're choosing to remove the dialog immediately after creating it. Looks problematic to me.
The whole reason Activity
exposes the showDialog()
, dismissDialog()
, onCreateDialog()
, onPrepareDialog()
family of methods is to be able to efficiently manage the Dialog's lifecycle, and specifically so you don't have to manually hold on to references to your dialogs. The idiomatic lifecycle is you create the Dialog in onCreateDialog()
, including attaching any event listeners you need. Then in onPrepareDialog()
is where you initialize values, things like text, initial values, etc -- or in other words "refreshing" the dialog for an impending display. There should be no reason to destroy and recreate your dialog between displays. Instead of using removeDialog()
to "refresh" it, you should actually look into what it will take to properly refresh the contents. Usually that would involve something like .getAdapter().notifyDataSetInvalidated()
, or if using CursorAdapter
, calling requery()
to force it to rerun the query and update the adapter contents.
If you do find that you still need to remove and recreate the dialog, then I would recommend abandoning the entire Activity
-managed dialog idiom, and instead create the dialog when you need it and call its show()
method. When you're done with it, dismiss and enqueue for GC. That bypasses the entire show/create/prepare process that Activity exposes.
I would suggest sticking to the Activity-managed dialogs, but not if you're planning to remove the dialog every time -- in that case just instantiate and .show()
I think the idea of deleting the Dialog is an interesting approach rather than going through the overhead of making a custom adapter for lightweight dynamic lists. At first glance, deleting from within onPrepareDialog() may not be the best approach for most implementations though.
Another approach would be to delete the dialog only when the list's dynamic data is updated.
private final int DIALOG_ID = 1;
String mDialogList[] = {"item 1","item 2"};
private void updateListData(){
mDialogList = new String[]{"new item 1","new item 2"};
removeDialog(DIALOG_ID);
}
Using this approach the dialog will only be reconstructed after underlying data changes, otherwise it should be cached and managed by the Activity as normal.
精彩评论