Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment already added: DateDialog

I have make my editText clickable. Once it is clicked, it will display DatePicker dialog.

 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.information);
 date = (EditText) findViewById(R.id.date);
 dialog=new DateDialog();
 date.setOnClickListener(this);

        public void onClick(View arg0) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        dialog.show(ft, "DatePicker");
        }

 public static class DateDialog extends android.app.DialogFragment implements DatePickerDialog.OnDateSetListener {
        public DateDialog() {}

         public Dialog onCreateDialog(Bundle savedInstanceState)
        {
            final Calendar c=Calendar.getInstance();
            int year=c.get(Calendar.YEAR);
            int month=c.get(Calendar.MONTH);
            int day=c.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(),this,year,month,day);
        }

        public void onDateSet(DatePicker view,int year, int month, int day)
        {
            String date1=day+"-"+(month+1)+"-"+year;
            date.setText(date1);
            date2= date.getText().toString();
            return ;
         }
    }

If I accidentally double click on the editText, the app crashed and shows Fragment already added error as below.

11-26 10:16:04.745    2348-2348/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.myapplication, PID: 2348
    java.lang.IllegalStateException: Fragment already added: DateDialog{d1ca446 #0 DatePicker}
            at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1219)
            at android.app.BackStackRecord.run(BackStackRecord.java:715)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Does anyone know how to fix this ? Thanks

like image 963
Hoo Avatar asked Nov 26 '15 10:11

Hoo


2 Answers

the app crashed and shows Fragment already added error as below

Because make a call to show method. check for is Dialog is showing or not if showing then discard the click event otherwise show Dialog:

Dialog dialogFrg=dialog.getDialog();
if(dialogFrg!=null && dialogFrg.isShowing()) {
   // no need to call dialog.show(ft, "DatePicker");
} else {
   // call dialog.show(ft, "DatePicker");
}

or use can also use a boolean flag like a switch to check Dialog is showing or not by making flag true/false.

like image 83
ρяσѕρєя K Avatar answered Oct 16 '22 09:10

ρяσѕρєя K


you can use :

  if(!dialogFragment.isAdded())
    dialogFragment.show(getSupportFragmentManager(), "datePicker");
like image 3
Motawea Alhaw Avatar answered Oct 16 '22 10:10

Motawea Alhaw