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