I use dynamic fragments in my application. To load any of the fragments, simply click on the view to which the onclicklistner is attached. If you alternately press on these views very quickly, then one fragment is replaced by another with the same speed. In this case, my application crashes, because one of the fragments has AsyncTask, which does not have time to execute and falls on null in the place textview.setText (data from AsyncTask). So, are there any standard methods or libraries for downloading fragments after a certain time? For example, I clicked on the view, which launches fragment A, then immediately clicked on the view, which launches fragment B, but fragment B does not start, because, for example, 2 seconds did not pass. As soon as it takes 2 seconds, so I can click on the view to launch fragment B. And until 2 seconds have passed, you can click any time, the fragment will not start.

    1 answer 1

    In your case, you can offer other options.

    The first is when launching AsyncTask to prohibit changing fragments until it AsyncTask . ( setEnabled(false) for view )

    The second is to set a flag when starting up and not to allow fragments to change until AsyncTask ( if(flag){...} ) AsyncTask

    The third ugly is to wrap in a try-catch and not worry, but there are problems here that several asynctask can run.

    • It is possible and so, but in this case, one fragment will run quickly, and the second - after a while. The user may have some dissonance. And such an implementation is ugly from the point of view of symmetry. - Sergey Molyak
    • @SergeyMolyak can be notified that an action is being performed. In addition, is AsyncTask guaranteed to run in the allotted time? Well, or if you want so, you can run with postDelayed through a handler and again, the flag is useful here. - VAndrJ