Progress Dialog getting dismissed before displaying fetched data from firestore

I am fetching data from document and then displaying it on an activity, but it takes some time to display it so i wanted to put a dialog while the job is geting done. so i added progressdialog (i know it’s deprecated, i’ll use custom dialog afterwards), but the problem is dialog is getting called and is getting dismissed instantly even before data get displayed. i don’t know how to listen if data is got displayed successfully so i can put dismiss method after that. here’s code :

package com.example.firestooredemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class StudentProfileData extends AppCompatActivity {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    String S_ID,id;
    TextView Student,MArks,Time;
//    Timestamp t= new Timestamp("160536400","13000000000");
    Map<String, Object> Stud;
    String TAG = "Datacheck";
    Sdm Stud_Data=new Sdm();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_profile_data);

        ProgressDialog Pd=new ProgressDialog(this);
        Pd.setCancelable(false);
        Pd.setMessage("Loading Student Data ...");
        Pd.show();


        Student = findViewById(R.id.Stud_Name);
        Time = findViewById(R.id.Timestamp1);
        MArks = findViewById(R.id.MarksView);
        Intent intent = getIntent();
        S_ID = intent.getStringExtra("S_ID");
        id=intent.getStringExtra("Path");
        DocumentReference Stud_Doc= db.collection("EM_DEMO2").document("10th_STD").collection(S_ID).document(id);
        Stud_Doc.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                Stud = documentSnapshot.getData();
                Stud_Data.readData(Stud);

                Log.d(TAG,id+documentSnapshot.getData().toString());
                Student.setText(Stud_Data.getStudentname());
                Time.setText(Stud_Data.getDateTime());
                MArks.setText(Stud_Data.getMarks());

            }
        });
        Log.d(TAG,"YAAAY");

        if(Pd.isShowing()){
              Pd.dismiss();
        }

    }

}

the output looks like this : https://imgur.com/a/XoffOYq

that delay of 0.5 sec or something is the problem. please help, thanks in advance

Answer

The Progress Dialog dismissed instantly because you set Pd.dismiss() directly inside onCreate method.

Set Pd.dismiss() inside onSuccess so the Progress Dialog will be dismissed after finish fetching data from Firebase.

Progress Dialog is deprecated. You can use Progress Bar as its alternative. It’s easy to use, all you have to do are:

  1. define Progress Bar element in xml file
  2. set visibility to GONE or VISIBLE for the Progress Bar element in java file.

Here are some references for Progress Bar:

  1. Official documentation: https://developer.android.com/reference/android/widget/ProgressBar
  2. Example: https://abhiandroid.com/ui/progressbar
  3. Another use in stackoverflow: ProgressDialog is deprecated.What is the alternate one to use?