Multiple dialogs creation in Android is a common problem .Let's seek solution to it
Today we will discuss on how to avoid creation of multiple dialog from the same piece of code.
The key is to maintain a counter and if its 0 , that means no dialog is showing now then only show a dialog. And on every dismissal of the dialog we reduce that counter to maintain the count of the dialog shown . That means at one time it should be not more than one .
//the counter to maintain the showing status of the dialog
private static int dialog_count =0;
private Dialog dialog=null;
public void showAlertDialog(final Context context, Activity activity) {
ListAdapter arrayAdapter;
final ArrayList<Language> arraylist = new ArrayList<>();
Integer[] imageId = {
R.drawable.british_english_flag,
R.drawable.swedish_flag_small,
R.drawable.italian_flag
};
final String[] languages = context.getResources().getStringArray(R.array.language_list);
for (int i = 0; i < languages.length; i++) {
Language langObj = new Language(languages[i].substring(3, languages[i].length()), imageId[i]);
arraylist.add(langObj);
}
arrayAdapter = new LanguageArrayAdpater(activity, arraylist);
dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_layout, null);
ListView lv = (ListView) view.findViewById(R.id.listView_language);
TextView tv_header = (TextView) view.findViewById(R.id.dialog_header);
tv_header.setTypeface(Utility.setTypeFace(Defines.font_OpenSansRegular));
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedVal = arraylist.get(position).getName();
Log.d(TAG, "chosen " + selectedVal);
dialog.dismiss();
//if an item is selected and the dialog was showing then reduce the dialog counter and perform the required action
if(dialog_count>0)
dialog_count--;
saveInPrefs(languages,context, position, selectedVal);
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
System.out.println("dialog count dismissing :"+ dialog_count);
//if the dialog was cancelable then on dismissing it we reduce the count so that we have proper data in the counter
if(dialog_count>0)
dialog_count--;
System.out.println("dialog count dismissed :"+ dialog_count);
}
});
dialog.setContentView(view);
//show the dialog only if we have no dialog showing currently.
if(dialog_count==0) {
dialog.show();
dialog_count++;
}
Today we will discuss on how to avoid creation of multiple dialog from the same piece of code.
The key is to maintain a counter and if its 0 , that means no dialog is showing now then only show a dialog. And on every dismissal of the dialog we reduce that counter to maintain the count of the dialog shown . That means at one time it should be not more than one .
//the counter to maintain the showing status of the dialog
private static int dialog_count =0;
private Dialog dialog=null;
public void showAlertDialog(final Context context, Activity activity) {
ListAdapter arrayAdapter;
final ArrayList<Language> arraylist = new ArrayList<>();
Integer[] imageId = {
R.drawable.british_english_flag,
R.drawable.swedish_flag_small,
R.drawable.italian_flag
};
final String[] languages = context.getResources().getStringArray(R.array.language_list);
for (int i = 0; i < languages.length; i++) {
Language langObj = new Language(languages[i].substring(3, languages[i].length()), imageId[i]);
arraylist.add(langObj);
}
arrayAdapter = new LanguageArrayAdpater(activity, arraylist);
dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_layout, null);
ListView lv = (ListView) view.findViewById(R.id.listView_language);
TextView tv_header = (TextView) view.findViewById(R.id.dialog_header);
tv_header.setTypeface(Utility.setTypeFace(Defines.font_OpenSansRegular));
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedVal = arraylist.get(position).getName();
Log.d(TAG, "chosen " + selectedVal);
dialog.dismiss();
//if an item is selected and the dialog was showing then reduce the dialog counter and perform the required action
if(dialog_count>0)
dialog_count--;
saveInPrefs(languages,context, position, selectedVal);
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
System.out.println("dialog count dismissing :"+ dialog_count);
//if the dialog was cancelable then on dismissing it we reduce the count so that we have proper data in the counter
if(dialog_count>0)
dialog_count--;
System.out.println("dialog count dismissed :"+ dialog_count);
}
});
dialog.setContentView(view);
//show the dialog only if we have no dialog showing currently.
if(dialog_count==0) {
dialog.show();
dialog_count++;
}
No comments:
Post a Comment