Home > Software design >  When and why override onResume method in MainActiviy?
When and why override onResume method in MainActiviy?

Time:06-17

What is the reason to overide onResume? Can we avoid it?

@Override
protected void onResume() {
    super.onResume();

When is mandatory to override that method in MainActiivity? Because sometimes I see it is used sometimes not.

CodePudding user response:

first of all you must know about android lifecycle link https://abhiandroid.com/programming/activity-life-cycle

onResume(); can be called in two different places after onStart(); when the user start interacting with your activity and data in the activity is visible to him or after onPause(); when your activity goes to the background then you can implement it here to add some logic which is needed for your application .

CodePudding user response:

Please reference this link:

https://developer.android.com/guide/components/activities/activity-lifecycle#onresume

When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off.

When the activity moves to the resumed state, any lifecycle-aware component tied to the activity's lifecycle will receive the ON_RESUME event. This is where the lifecycle components can enable any functionality that needs to run while the component is visible and in the foreground, such as starting a camera preview.

When an interruptive event occurs, the activity enters the Paused state, and the system invokes the onPause() callback.

If the activity returns to the Resumed state from the Paused state, the system once again calls onResume() method. For this reason, you should implement onResume() to initialize components that you release during onPause(), and perform any other initializations that must occur each time the activity enters the Resumed state.

  • Related