1

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.

  • I recommend reading this https://developer.android.com/guide/components/activities/activity-lifecycle – SaNtoRiaN Jul 25 '21 at 17:07
  • A component might be stopped and started several times if the user switches away and back. – chrylis -cautiouslyoptimistic- Jul 25 '21 at 17:11
  • 1
    Overriding `onStart` and `onStop` used to be much more relevant before `androidx.lifecycle` became a thing, nowadays most components that need to be aware of activitys state will just observe lifecycle themselves and don't require explicit notifications. – Pawel Jul 25 '21 at 17:47
  • @SaNtoRiaN I've read through the doc before but it doesn't really clean up the confusion regarding the purpose of onStart if onCreate is called anyway. – StellarEquilibrium Jul 25 '21 at 18:18
  • @Pawel so developers don't need to worry about it anymore? Just shove everything into onCreate? This doesn't sound right. – StellarEquilibrium Jul 25 '21 at 18:19

1 Answers1

7

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 (e.g., 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().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The following diagram could also be useful: https://developer.android.com/guide/components/images/activity_lifecycle.png – anemomylos Jul 25 '21 at 17:59
  • I've seen the diagram but my questions about when to do what remain. – StellarEquilibrium Jul 25 '21 at 18:17
  • @CommonsWare You said onStart is called after an activity takes over the screen but in my Log.i onCreate is called as well when switching Activities (back). Also when you said onStart has code when either the activity is created etc... then my brain simply goes back to the questions in the OP-Thread. What is the point of onStart if it does what onCreate is intended to? – StellarEquilibrium Jul 25 '21 at 18:17
  • 1
    @StellarEquilibrium: Then you are doing something to cause that to happen. – CommonsWare Jul 25 '21 at 18:21
  • @CommonsWare I put log.i in the onCreate, onStart and onResume method, each with their own "on... called" text. – StellarEquilibrium Jul 25 '21 at 18:22
  • 1
    @StellarEquilibrium: Sorry, I meant that you are doing something that is causing `onCreate()` to be called when you think that it should not (e.g., you turned on "Don't keep activities" in the Developer portion of Settings). You might consider asking a separate Stack Overflow question, where you provide a [mcve] showing what you are trying and show your logs with your results. – CommonsWare Jul 25 '21 at 18:26
  • @CommonsWare I agree though I feel like this could go on forever depending on how you stretch it. Let me simplify it to my case then: I have a bunch of data fetched from the internet into Arraylists and then binded & shown in a recyclerview. - In my case, correct me if I'm wrong but the data is fetched and the views are declared in the onCreate method correct? - In the onStart Method the views are initialized, the data is added to the arraylists & the recyclerview is binded / Filled up with the arraylists. - In the onResume method the recyclerview is shown? – StellarEquilibrium Jul 25 '21 at 18:29
  • 2
    @StellarEquilibrium: I do not know anyone who would approach the problem that way, particularly in the past few years. In the end, you are welcome to do whatever you like, if it works for you. It feels like you are trying to learn Android app development by reading a handful of 10-year-old posts and blending that with slightly newer bits (e.g., `RecyclerView`). It may be worth you spending some time with a newer book or course, one that can show you how things are done in a more organized and up-to-date fashion. – CommonsWare Jul 25 '21 at 18:41
  • 1
    @StellarEquilibrium: To put it another way, implementing `onStart()` or `onResume()` in an activity is a bit of a "code smell" in modern Android app development. As others have hinted, most likely there are better solutions for whatever problem you are trying to solve. But teaching you modern Android app development is well beyond a single Stack Overflow question and answer, which is why I suggest you seek out books and courses that can help you "spin up" on modern development techniques. – CommonsWare Jul 25 '21 at 18:43
  • You're 100% correct about the 10yr old posts and modern snipping & stitching. I'll choose your answer as the accepted solution. – StellarEquilibrium Jul 25 '21 at 18:53