简体   繁体   中英

Where is the difference between onCreate and onStart if both are called upon Activity change anyway? What's the purpose?

I've searched through dozens of Stackoverflow posts and the android doc but just couldn't find the answer.

According to the accepted answer of this SF-post the onCreate method runs when the activity is first created. It also notes that in here views are supposed to be created and list data is being binded.

Then the onStart Method runs but here's the issue. Where's the difference? If you do everything inside of onCreate, switch activities, your app will still display the same data, regardless whether you put the app in the background or switched activities.

So if you declare views in onCreate, what do you do in onStart? initiliaze the views to their R.id.view ? Fetch data?

onResume I suppose is then used for listeners since it's the gas and brake according to this SF-posts accepted answer .

onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate() ) and after it is coming back to the screen from being stopped (eg, another activity took over the screen).

So:

  • Put code in onCreate() that needs to happen when the activity is created (and use onDestroy() to clean it up)

  • Put code in onStart() that needs to happen either when the activity is created or when the activity returns to the foreground (and use onStop() to clean it up)

Frequently, we do not do anything special when the activity returns to the foreground, in which case you do not need to worry about onStart() or onStop() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM