QWERTY
QWERTY

Reputation: 2315

progressDialog gets dismissed before fragment opened up

I was having some problem when trying to dismiss ProgressDialog after opened up a new fragment. Here is my code:

        ProgressFragment progressDialog = new ProgressFragment();
        progressDialog.setProgressMessage("Sending command, please wait.");
        progressDialog.show(fragment.getFragmentManager(), null);
        Handler handler = new Handler();
        handler.postDelayed(() -> {
            if (true) {
                ResultFragment resultFragment = new ResultFragment_();
                resultFragment.show(getFragmentManager(), null);
                progressDialog.dismiss();
            }
            else{
                // show error toast
            }
        }, 5000);

With the code above, the progressDialog got dismissed before the new fragment opened up. Is there any way to set it such that the new fragment opened up, then dismiss the progress dialog.

Thanks!

Upvotes: 1

Views: 124

Answers (2)

Abhay Koradiya
Abhay Koradiya

Reputation: 2117

Very easy Idea is send the message through handler to Activity and close progress-dialog. Like,

Fragment OnCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   Message msg = new Message();
   msg.what = 1223;
   Activity1.mHandler.sendMessage(msg);

}

in Activity

 public static Handler mHandler = new Handler(new Handlercontext.Callback() {
    @Override
    public boolean handleMessagegetClass(Message msg) {

     if(msg.what == 1223){
          progressDialog.dismiss();
     }

     return false;
    }
});

Upvotes: 1

Aiko West
Aiko West

Reputation: 791

Declare in Fragment A

private ProgressFragment progressDialog;

and

public void CloseProgressFragment()
{
    progressDialog.dismiss();
}

do not dismiss the dialog in postDelayed

in Fragment B get your Fragment A with the help of the FragmentManager and call the method to close

string fragATag = typeof(FragmentA).Name

FragmentA fragmentA = FragmentManager.FindFragmentByTag(fragATag);

fragmentA.CloseProgressFragment();

Upvotes: 0

Related Questions