user3875913
user3875913

Reputation: 252

When does "onResume()" run without "onStart()"?

During my testing, I have not found a situation where onStart() runs without onResume().

If someone could shed light on this topic as this is the closest question I've found, but none of the answers address the start/resume part just the stop/pause part.

If there is no relevant situation, is it ok to omit onStart() or onResume() and not use both as that seems redundant?

Upvotes: 0

Views: 889

Answers (2)

TheWanderer
TheWanderer

Reputation: 17854

There's documentation on it.

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

As I understand it, onStart() and onStop() represent visibility, while onResume() and onPause() represent priority.

For example, if you open your app, both onStart() and onResume() will be called. With your app still open, say you then get a Facebook Message and open the chat. onPause() will be called, but onStop() won't. Your app is no longer in the foreground, but it's still visible.

EDIT:

I know I linked the Activity documentation, but according to the Fragment documentation:

onStart() makes the fragment visible to the user (based on its containing activity being started).

onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).

onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.

onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.

The same principle applies. In most cases, it's just a direct call from the Activity.

Upvotes: 2

EpicPandaForce
EpicPandaForce

Reputation: 81549

Two examples off the top of my head:

1.) System dialog opens over your app (for example, via Intent.createChooser) but cancelling the dialog

2.) Multi-window mode, tapping on the other application then tapping on yours (you will receive onPause/onResume but not onStop/onStart)


In my experience, the only time you actually need onPause() is if you are writing your own camera.

If you're trying to show a DialogFragment after onPause, you generally need to wait until onResumeFragments/onPostResume.

Upvotes: 1

Related Questions