google-developer-training / android-basics-kotlin-cupcake-app

Apache License 2.0
103 stars 163 forks source link

Android Basics: Shared ViewModel #22

Closed Mohammed711 closed 3 years ago

Mohammed711 commented 3 years ago

URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-training-shared-viewmodel?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-3-pathway-4%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-shared-viewmodel#2

In which task and step of the codelab can this issue be found? -Shared ViewModel Across Model

Describe the problem I am not able to change the title of the app bar

Screenshot 2021-06-25 at 11 54 27 AM

Steps to reproduce?

  1. Go to...
  2. Click on...
  3. See error...

Versions Android Studio version: 4.2.1 API version of the emulator: 30_x86

Additional information Include screenshots if they would be useful in clarifying the problem.

sdhong0609 commented 3 years ago

I answered this question before with another github account, but I deleted the answer because the account is for only my company account. So I deleted answer and now I answer again with my personal account. The answer is same with before.

I experienced same problem and the difference of codelab is onCreate() method in MainActivity...and I found the problem and fixed it. There are two onCreate(), and I overrided another onCreate().

Override this. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }

not this. override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { super.onCreate(savedInstanceState, persistentState) }

bacon-cy commented 3 years ago

@hong-studio I'm curious about this, why are there two onCreate()? Are they different functions?

kkuan2011 commented 3 years ago

The two onCreate() methods should be the same with different parameters, so you could use either one.

As for setting title, you can call this within the activity: https://developer.android.com/reference/android/app/Activity#setTitle(java.lang.CharSequence)

Or change the app name in the strings.xml file.

If you have any issues, feel free to reopen.

AnneJ17 commented 3 years ago

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { super.onCreate(savedInstanceState, persistentState) }

I noticed that when using the above onCreate() , the up button won't show up. Can someone explain the reason behind this? However, the up button shows if we use the below onCreate().

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }