I have 1 Activity showing 1 DialogFragment and I use setAlwaysFinish app to force the Activity to get killed after onPause.
After pausing & restoring the Activity, I sometimes got 2 or 3 of the same DialogFragment shown. Does anybody know how to prevent this?
Note: I left the Activity with the Dialog still showing. The bug's revealed after several trying with setAlwaysFinish.
Code: Activity
package com.example.testdialog;
import java.util.Calendar;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDialog(View v) {
Calendar today = Calendar.getInstance();
DialogFragment d = DateDialog.newInstance(today.get(Calendar.DAY_OF_MONTH), today.get(Calendar.MONTH), today.get(Calendar.YEAR));
d.show(getSupportFragmentManager(), "mydialog");
}
}
Code: DialogFragment
package com.example.testdialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
public class DateDialog extends DialogFragment implements OnDateSetListener {
public static final String TAG = "datepicker";
public static final int SEARCH_FLIGHT = 0;
public static final int PASSENGER = 1;
static DateDialog newInstance(int day, int month, int year) {
DateDialog d = new DateDialog();
Bundle bun = new Bundle();
bun.putInt("day", day);
bun.putInt("month", month);
bun.putInt("year", year);
d.setArguments(bun);
return d;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bun = getArguments();
int year = bun.getInt("year");
int month = bun.getInt("month");
int day = bun.getInt("day");
DatePickerDialog d = new DatePickerDialog(getActivity(), this, year, month, day);
d.setButton(DialogInterface.BUTTON_POSITIVE, "OK", d);
d.setTitle("my dialog");
return d;
}
@Override
public void onDestroyView() {
Dialog d = getDialog();
if (d != null && getRetainInstance())
d.setDismissMessage(null);
super.onDestroyView();
}
}
You can prefent it by using this in your dialogfragment:
@Override
public void show(FragmentManager manager, String tag) {
if (tag != null && tag.equals("FRAGMENT_TAG_MAX_ONE_INSTANCE")) {
// we do not show it twice
if (manager.findFragmentByTag(tag) == null) {
super.show(manager, tag);
}
} else {
super.show(manager, tag);
}
}
and changing:
d.show(getSupportFragmentManager(), "FRAGMENT_TAG_MAX_ONE_INSTANCE");
I have same issue and dont know real way to solve it but this way can help you
private void showLostPassDialog() {
if (!dialogShown) {
dialogShown = true;
DialogFragment newFragment = new LostPassDialogFragment();
newFragment.setCancelable(false);
newFragment.show(getSupportFragmentManager(), "dialog");
}
}
and when destroy dialog set dialogShown=false
as i did :
public static class LostPassDialogFragment extends SherlockDialogFragment {
....
@Override
public void onDestroy() {
// TODO Auto-generated method stub
dialogShown = false;
super.onDestroy();
}
hope to be useful
Whenever you close the dialog, close it properly by calling
dialog.dismiss();
dialog.cancel();
For me this same problem occurred when I used getDialog().setOnDismissListener()
inside DialogFragment
.
After going through DialogFragment
's documentation I found this:
Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself. To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface) Source: here
Removing getDialog().setOnDismissListener()
and implementing onDismiss(DialogInterface)
fixed this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.