Hello, I encountered such a problem: There is a fragment from which the Activity is launched with the following topic:

<style name="AppThemeDialog" parent="Theme.AppCompat.Light.Dialog"> <!-- Customize your theme here. --> <item name="windowNoTitle">true</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:splitMotionEvents">false</item> <item name="android:windowEnableSplitTouch">false</item> <item name="android:soundEffectsEnabled">false</item> </style> 

The launch code for this activation is also quite simple:

 public void onClick(View v) { MusicManager.continueMusic = true; Intent intent = new Intent(v.getContext(), UnlockPartActivity.class); getActivity().overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); BlockFragment.this.startActivityForResult(intent, REQUEST_UNLOCK); } 

This Activity starts and everything seems to be fine. But if you quickly double-click on the button that starts this activity, it will open twice. This should not be. It only comes to mind to temporarily block a button or remove a handler before exiting activation. However, can there be more correct ways to correct this behavior?

    1 answer 1

    Direction of thoughts is correct, from the "Fastest shooters in the Wild West", in the case of Activity , it is possible to escape by blocking clicks by entering boolean lock; -variable or setting .setClickable(false) on the required component View .

    In the case of Fragment -s, you can enter a check for the existence of an already running Fragment with the same tag . And the launch of Fragment provide yourself:

     public class MyDialogFragment extends DialogFragment { ... public void show(FragmentManager fragmentManager) { String tag = getClass().getSimpleName(); if (fragmentManager.findFragmentByTag(tag) == null) { show(fragmentManager, tag); } } ... } 

    The heir to the DialogFragment is used as an example; the same practice works on the direct heirs of Fragment .

    Although among the developers I noticed the frequent practice of ignoring the presence of the possibility of multiple launch.

    • The dialogue is not made in the form of a fragment but in the form of activity I know about tags in fragments and they are even used. But since the dialogue style was needed, it was decided to use the Activity. - Nikita Vasilchenko
    • @ NikitaVasilchenko DialogFragment I used as an example to indicate that there is no difference in using this method for Fragment and DialogFragment, they have one root - Fragment :) - Artilirium