Android SDK equivlent for viewWillAppear (iOS)

androidoncreatesharedpreferencestextviewviewwillappear

Situation

I have a fairly simple app with 2 "layouts" using SharedPreferences.

  • Main.xml
  • Settings.xml

Main has a textView that uses getString from SharedPreferences. Also a button to open Settings.

Settings has a spinner and a button to save to SharedPreferences.


The textView gets updated when the App loads as I am calling setText() inside onCreate(Bundle savedInstanceState)

Problem

When I open Settings and update the SharedPreferences, I use the Back button to get back to Main.

Since I am calling setText() inside onCreate() the textView does not update again until I exit the app and open the app again to main.

What method do I need to use to update the textView after coming back from Settings?

My request is similar to the viewWillAppear() for iOS.

Best Answer

onCreate() is only called once when the activity first starts. If you want to update text whenever the activity becomes active you can use onResume() instead. More information on the Activity lifecycle can be found here.