Question

I was wondering what is the difference between the both methods, I meant, when is the best time to use one instead the other and why?

Was it helpful?

Solution

When OnResume gets called ?

User is in Activity 1 and now traverses to Activity 2. (Activity 1 is not visible at all). Now User clicks back button from Activity 1, OnResume method of Activity 1 gets called.

When OnRestart gets called ?

When user traverses from Activity 1 to Activity 2 as in above case, moves back to Activity 1, onRestart gets called.

so what is the difference ?

OnResume method gets called everytime when an activity moves between background to foreground state.

But, onRestart gets called only when onStop method is called.

Please note that, the system also calls the onStart() method, which happens every time your activity becomes visible (whether being restarted or created for the first time). The onRestart() method, however, is called only when the activity resumes from the stopped state.

When to use OnRestart Method ?

Use this when you want to perform special restoration work that might be necessary only (if you have handled onStop method for releasing ay CPU intensive resources) if the activity was previously stopped, but not destroyed.

OTHER TIPS

OnResume Method is called after the Creation of the activity and it be Shown to user so , it useful when you try to connect to your resources that you disable in OnPause Method

second if you have 2 activity and you transfer to second activity the first activity in stopping state so when you return back to it you don't need to creating it from scratch you need only to restart it so Restart will call start method , then onresume method , so in most cases you deal with onresume directly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top