How and when to override Android Activity’s Lifecycle methods?

Read this article on my blog for a better experience 🙂

Yesterday, I came across a StackOverflow question.

The OP(original poster) of the article had a scenario like this:

He made a ProgressDialog in an activity. After pressing a CardView in that …


This content originally appeared on DEV Community and was authored by Gourav Khunger

Read this article on my blog for a better experience :)

Yesterday, I came across a StackOverflow question.

The OP(original poster) of the article had a scenario like this:

He made a ProgressDialog in an activity. After pressing a CardView in that activity corresponding to a meditation session, a ProgressDialog should appear for 3 seconds, and then the other activity would open - the m1 activity.

The problem he faced was - After returning to MeditationActivity, the ProgressDialog continued to show up and never stopped.

He wanted that the progress dialog is closed once the user comes back from the m1 activity to the MeditationActivity.

Just after reading the question, it should strike to the mind that one can override the default methods present in an Activity's Lifecycle and dismiss the dialog from there!

Here's how the lifecycle of an Android Activity looks like:

activity_lifecycle.png

This is an image from the official android documentation

From the flowchart, we can see that it is the onResume() method that is called whenever a person comes back to the activity from another screen. So it seems to be the place where we should place the logic for dismissing the dialog!

activity_lifecycle.png

Here's how we can do that:

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

The null check is placed because the progressDialog was initialized only when the person presses the CardView. And if it is initialized earlier, then there would be redundant dismissals of the progressDialog.

This was a perfect example where we could override the default lifecycle methods provided by an Android Activity to perform actions pertaining to lifecycle.

Here's my answer to the original question on StackOverflow! I'll be writing more explanations to StackOverflow Questions, so consider following me on hashnode.

I hope you find this article helpful :)


This content originally appeared on DEV Community and was authored by Gourav Khunger


Print Share Comment Cite Upload Translate Updates
APA

Gourav Khunger | Sciencx (2021-09-06T08:23:50+00:00) How and when to override Android Activity’s Lifecycle methods?. Retrieved from https://www.scien.cx/2021/09/06/how-and-when-to-override-android-activitys-lifecycle-methods/

MLA
" » How and when to override Android Activity’s Lifecycle methods?." Gourav Khunger | Sciencx - Monday September 6, 2021, https://www.scien.cx/2021/09/06/how-and-when-to-override-android-activitys-lifecycle-methods/
HARVARD
Gourav Khunger | Sciencx Monday September 6, 2021 » How and when to override Android Activity’s Lifecycle methods?., viewed ,<https://www.scien.cx/2021/09/06/how-and-when-to-override-android-activitys-lifecycle-methods/>
VANCOUVER
Gourav Khunger | Sciencx - » How and when to override Android Activity’s Lifecycle methods?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/06/how-and-when-to-override-android-activitys-lifecycle-methods/
CHICAGO
" » How and when to override Android Activity’s Lifecycle methods?." Gourav Khunger | Sciencx - Accessed . https://www.scien.cx/2021/09/06/how-and-when-to-override-android-activitys-lifecycle-methods/
IEEE
" » How and when to override Android Activity’s Lifecycle methods?." Gourav Khunger | Sciencx [Online]. Available: https://www.scien.cx/2021/09/06/how-and-when-to-override-android-activitys-lifecycle-methods/. [Accessed: ]
rf:citation
» How and when to override Android Activity’s Lifecycle methods? | Gourav Khunger | Sciencx | https://www.scien.cx/2021/09/06/how-and-when-to-override-android-activitys-lifecycle-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.