Pressing date input button fast tries to add DatePickerDialog fragment twice

Bug #1262744 reported by pjv
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Collectionista
Fix Committed
Low
pjv

Bug Description

In a (Tabbed) BookItem, if you press the acquired date button repeatedly and fast, then at some point it will crash with: IllegalStateException: Fragment already added: BookItemAcquisitionDatePickerDialog

From monkey:
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=net.lp.collectionista/.ui.activities.SplashScreen;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=net.lp.collectionista/.ui.activities.SplashScreen } in package net.lp.collectionista
:Sending Touch (ACTION_DOWN): 0:(284.0,271.0)
:Sending Touch (ACTION_UP): 0:(288.2498,269.12637)
// CRASH: net.lp.collectionista (pid 6652)
// Short Msg: java.lang.IllegalStateException
// Long Msg: java.lang.IllegalStateException: Fragment already added: BookItemAcquisitionDatePickerDialog{41fb1230 #9 acquisition_date_picker_dialog}
// Build Label: samsung/GT-P7510/GT-P7510:4.0.4/IMM76D/UELPL:user/release-keys
// Build Changelist: eng.root.20121017.195315
// Build Time: 1350518027000
// java.lang.IllegalStateException: Fragment already added: BookItemAcquisitionDatePickerDialog{41fb1230 #9 acquisition_date_picker_dialog}
// at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1175)
// at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
// at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
// at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
// at android.os.Handler.handleCallback(Handler.java:615)
// at android.os.Handler.dispatchMessage(Handler.java:92)
// at android.os.Looper.loop(Looper.java:137)
// at android.app.ActivityThread.main(ActivityThread.java:4931)
// at java.lang.reflect.Method.invokeNative(Native Method)
// at java.lang.reflect.Method.invoke(Method.java:511)
// at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
// at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
// at dalvik.system.NativeStart.main(Native Method)
//
** Monkey aborted due to error.
Events injected: 3684
:Sending rotation degree=0, persist=false
:Dropped: keys=23 pointers=37 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=103913ms (0ms mobile, 0ms wifi, 103913ms not connected)
** System appears to have crashed at event 3684 of 10000 using seed 10

Related branches

pjv (pjv)
Changed in collectionista:
status: New → Fix Committed
importance: Undecided → Medium
assignee: nobody → pjv (pjv)
Revision history for this message
pjv (pjv) wrote :

It seems some bug with the FragmentsManager or DialogFragment in the v4 support library. All you need to do is keep hammering the watch button to the right on the Meta tab.

Somehow, the previous fragment tapped away by clicking on the target of the button in the shadow is not always entirely gone( ft.remove()?) yet when another click on the button wants to add it (ft.add() or ft.replace()?).

I have tried code such as this and it seems to work better (or maybe my rhythm is off) but still fails (maybe needs to be inside some critical section (synchronized)?):

 public void onClick(View v) {...
               if (v.getId() == R.id.b_acquisition_date_change) {
   // Launch dialog to change acquisition date ....
   Fragment f = getActivity().getSupportFragmentManager().findFragmentByTag("acquisition_date_picker_dialog");
   if (f!=null){
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction()
               .remove(f);
    ft.commit();
   } else {
    mAcquisitionDatePickerDialogFragment.show(getActivity().getSupportFragmentManager(), "acquisition_date_picker_dialog");
   }
  }
 }

Could not find this bug online yet, and it is only really visible with the monkey.

Changed in collectionista:
status: Fix Committed → Confirmed
importance: Medium → Low
Revision history for this message
pjv (pjv) wrote :

I have also checked that I don't create a new DialogFragment each time and don't clean up the old one. Indeed, that is the case seemingly.

    @Override
 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
...
  if(mAcquisitionDatePickerDialogFragment != null && mAcquisitionDatePickerDialogFragment.getShowsDialog()) mAcquisitionDatePickerDialogFragment.dismissAllowingStateLoss();
  mAcquisitionDatePickerDialogFragment = new BookItemAcquisitionDatePickerDialog();
...
 }

I added the check before to dry and dismiss the old one first. However this immediately here gives another error:

12-19 18:25:39.198: E/AndroidRuntime(19524): java.lang.NullPointerException
12-19 18:25:39.198: E/AndroidRuntime(19524): at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:184)
12-19 18:25:39.198: E/AndroidRuntime(19524): at android.support.v4.app.DialogFragment.dismissAllowingStateLoss(DialogFragment.java:165)
12-19 18:25:39.198: E/AndroidRuntime(19524): at net.lp.collectionista.ui.fragments.items.book.TabbedBookItemEditFragment.onLoadFinished(TabbedBookItemEditFragment.java:418)

This should not be.

Revision history for this message
pjv (pjv) wrote :

Seem to have fixed the main problem by adding this check in front:

if (!mAcquisitionDatePickerDialogFragment.isAdded()) mAcquisitionDatePickerDialogFragment.show(getActivity().getSupportFragmentManager(), "acquisition_date_picker_dialog");

Not cleaning up the old fragment in onLoadFinished is acceptable.

Changed in collectionista:
status: Confirmed → Fix Committed
Revision history for this message
pjv (pjv) wrote :

Not making the fix yet on occurrences of "Fragment.show(" where the fragment is a local variable. Tested in one place with searchAdd and its add dialog and couldn't reproduce there anyway.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.