progressDialog gets dismissed before fragment opened up


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!




2 Answers
2



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;
}
});





Sorry but what I am trying to do is in first fragment, I opened up progressDialog. Then when switching over to fragment 2, I dismiss the progressDialog after fragment 2 shows up
– hyperfkcb
Jul 3 at 5:54





you can apply this logic for that.
– Abhay Koradiya
Jul 3 at 6:01





hyperfkck, remove progressDialog.dismiss() from your code and add this code.
– Sanchita Santra
Jul 3 at 7:30


progressDialog.dismiss()



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();





I do not think 'var' is supported in Android java :(
– hyperfkcb
Jul 3 at 5:49





see edits (use type string and type FragmentA)
– K. Dexter
Jul 3 at 5:51






I am getting cannot resolve typeof error message
– hyperfkcb
Jul 3 at 5:52






This code is Xamarin.Android. But i think it shows the main idea to solve your problem. Declare a public method to close the dialog and get your Fragment A in Fragment B (i think it is easy to find a way to do this in java). Then call the public method to close it
– K. Dexter
Jul 3 at 5:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height