0

I made a ProgressDialog for a meditation app. After pressing the Cardview corresponding to the meditation session, a ProgressDialog appears for 3 seconds, and then the other activity opens(m1 activity). But there is one big problem. After returning to MeditationActivity the ProgressDialog shows and never stops.

This is the code I am using:

public class Meditation extends AppCompatActivity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMeditationBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

findViewById(R.id.Med_1).setOnClickListener(v -> {
        startActivity(new Intent(getApplicationContext(), m1.class));
        progressDialog = new ProgressDialog(Meditation.this);
        progressDialog.show();
        progressDialog.setContentView(R.layout.loading_screen_first_version);
    });
}
    @Override
public void onBackPressed() {
    super.onBackPressed();
    progressDialog.dismiss();
}
}

Some of m1 code:

public class m1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_m1);

    findViewById(R.id.backm).setOnClickListener(v -> {
        onBackPressed();
        mediaPlayer.stop();
        notificationManager.cancelAll();
    });
Diligent PD
  • 101
  • 8
  • Why are you referencing views from their id by `R.id.` if you are using view binding? You must consider using `binding.` to reference the views. – Gourav Sep 05 '21 at 14:33

2 Answers2

1

From what I can judge, I think you want the progress dialog to hide when the user returns from the m1 activity back to the meditation activity.

For that you can override the onResume() method, because that is called when an activity comes to the foreground from an inactive state. You should add this in the Meditation activity:

@Override
public void onResume(){
    super.onResume();
    if(progressDialog != null) progressDialog.dismiss();
}

@Yunis's answer doesn't work because onPause is also called when an activity is preparing to go to the back ground so it doesn't show the dialog at all!

And btw, ProgressDialog is deprecated, you might want to look for alternatives :)

Gourav
  • 2,746
  • 5
  • 28
  • 45
  • Thank you for the suggestion, But I get the same error I got last time: void android.app.ProgressDialog.dismiss()' on a null object reference. – Diligent PD Sep 05 '21 at 14:38
  • I found a solution, I placed progressDialog.dismiss(); in onRestart and it worked. Maybe this edited code worked too. Thank you for your help! – Diligent PD Sep 05 '21 at 14:43
  • 1
    The problem was `onResume()` is also called before activity is fully able to handle UI, but the progress dialog is initialised only when the button gets clicked, so it gave null pointer. Checking for null exception in the code resolved it :) – Gourav Sep 05 '21 at 14:44
  • @DiligentPD Do read [this](https://genicsblog.com/how-and-when-to-override-android-activity-lifecycle-methods). – Gourav Sep 06 '21 at 11:32
0

You dismiss progressDialog in wrong place. You can dismiss progress dialog for example inside onPause() method of activity

@Override
    protected void onPause() {
        super.onPause();
        progressDialog.dismiss();
    }
Yunis Rasulzade
  • 325
  • 3
  • 12