11

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.

4 Answers4

20

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.

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
SiPe
  • 242
  • 3
  • 7
  • For now I have used Simon's responce with onResume(). @Override protected void onResume() { super.onResume(); // setText() here } – David Todd Watson Dec 07 '11 at 19:09
  • I also found this while digging around after submitting my question :( [onResume() update TextView](http://stackoverflow.com/questions/6890432/onresume-update-textview) – David Todd Watson Dec 07 '11 at 19:34
1

You can also create an OnSharedPreferenceChangeListener. Register it in your Activity.onCreate() with PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(). Also unregister it in Activity.onDestroy() with PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener().

Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
Anasthase
  • 491
  • 2
  • 7
  • This sounds interesting as well. I see the potential problem with using onResume() and running extra code each time the main screen appears. – David Todd Watson Dec 07 '11 at 20:18
  • One more thing if you use this, always keep a reference on the listener, otherwise it may not work. you need the reference anyway to unregister it ;) – Anasthase Dec 07 '11 at 20:36
0

You could also start your Settings activity with startActivityForResult and then implement an onActivityResult method in your Main activity: StartingActivities

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
Chris
  • 3,007
  • 2
  • 19
  • 21
  • I am using the startActivityForResult() with Intent already, is your method better then? – David Todd Watson Dec 07 '11 at 19:13
  • I wouldn't say it's better. OnResume will be called at other times than just when you get back from Settings. If you only do the update in your onActivityResult method then you avoid unnecessarily checking / updating the field in the onResume. That being said, it's somewhat of a personal preference call. – Chris Dec 07 '11 at 19:19
  • Ok I will take a look at performance and other issues to see which solution is best... I really appreciate the help. – David Todd Watson Dec 07 '11 at 19:32
-3

the best solution I found is to override onVisibilityChanged

@Override
protected void onVisibilityChanged(View changedView, int visibility){
    super.onVisibilityChanged(changedView, visibility);
    if(visibility==VISIBLE ){
        Log.v("View will appear", "");  
    }
    else{
        Log.v("View will disappear", "");   
    }
}
Freddie
  • 29
  • 4