Search code examples
androidandroid-viewpagerdialogfragment

Android: Avoid opening the dialog several times, but allow hide and shows the same dialog


I have an Activity with ViewPager PagerSlidingTabStrip for each page of my ViewPager has a fragment, and in each fragment realize an http request (using Volley) to load the data from the page, but when the request ends in error, type timeout or lost connection, I need to display a dialog with the option to redo the call to the server, the problem to prevent multiple dialogs are open for each error is resolved with the snippet:

See this solution here: http://www.jorgecoca.com/android-quick-tip-avoid-opening-multiple-dialogs-when-tapping-an-element/

@Override
public void show(FragmentManager manager, String tag) {
    if (manager.findFragmentByTag(tag) == null) {
      super.show(manager, tag);
    }
}

When the user clicks the dialog button to try again, and the dialog closed and taken to check if there is internet connection, if I'm not, the dialog should be opened again, but the dialog is not displayed again, I believe that the tag does not was released to FragmentManager.

Code in Activity:

final Button mButton = ( Button ) this.findViewById( R.id.btn_opendialog );

final DialogFragmentHelper mDialog = new DialogFragmentHelper();
mDialog.setCallbackListener( new OnCallback() {

    @Override
    public void onCancel() {

    }

    @Override
    public void onConfirm() {

        // verify if network available

        mDialog.show( MainActivity.this.getSupportFragmentManager(), DialogFragmentHelper.DIALOG_TAG );
    }
} );

mButton.setOnClickListener( new OnClickListener() {

    @Override
    public void onClick( final View v ) {

        mDialog.show( MainActivity.this.getSupportFragmentManager(), DialogFragmentHelper.DIALOG_TAG );
    }
} );

Would someone have a suggestion of a workaround?


Solution

  • In order to maintain the structure that is ready in my project, and also keep something closer to my goal, which is to use no flags, nor pass control of a third dialogfragment to manage, arrived at a solution that'll take an hour as a result.

    DialogFragmentHelper mDialog = new DialogFragmentHelper();
    mDialog.setCallbackListener( new OnCallback() {
    
        @Override
        public void onCancel() {}
    
        @Override
        public void onConfirm() {
    
            mDialog.dismissAllowingStateLoss();
    
            if(networkAvailable == false){
    
                new Handler().post( new Runnable() {
    
                    @Override
                    public void run() {
                        mDialog.show( MainActivity.this.getSupportFragmentManager(), DialogFragmentHelper.DIALOG_TAG );
                    }
                } );
    
            }else {
                //do something here
            }
        }
    } );
    

    this way I guarantee that while several requests are sent to open the dialogfragment, only the first is executed, and closing the dialogfragment, I can quickly open it again if needed, as can happen in the scenario I'm working.