Android SDK equivlent for viewWillAppear (iOS)?
Asked Answered
H

4

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.

Halfcaste answered 7/12, 2011 at 18:53 Comment(0)
H
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.

Hound answered 7/12, 2011 at 18:58 Comment(2)
For now I have used Simon's responce with onResume(). @Override protected void onResume() { super.onResume(); // setText() here }Halfcaste
I also found this while digging around after submitting my question :( onResume() update TextViewHalfcaste
N
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().

Noneffective answered 7/12, 2011 at 19:46 Comment(2)
This sounds interesting as well. I see the potential problem with using onResume() and running extra code each time the main screen appears.Halfcaste
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 ;)Noneffective
I
0

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

Iridize answered 7/12, 2011 at 19:1 Comment(3)
I am using the startActivityForResult() with Intent already, is your method better then?Halfcaste
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.Iridize
Ok I will take a look at performance and other issues to see which solution is best... I really appreciate the help.Halfcaste
L
-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", "");   
    }
}
Lyophobic answered 13/4, 2012 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.