Android's viewDidLoad and viewDidAppear equivalent
Asked Answered
S

3

36

Does Android have an equivalent to Cocoa's viewDidLoad and viewDidAppear functions?

If not, then how would I go about performing an action when a View appears? My app is a tabbed application, in which one of the tabs is a list of forum topics. I would like the topic list to be refreshed every time the view appears. Is such a thing possible in Android?

Socrates answered 18/8, 2010 at 14:56 Comment(1)
An excellent old question! This may be helpful here: https://mcmap.net/q/428038/-difference-between-addonlayoutchangelistener-and-onlayout-changedSwitcheroo
S
24

The Activity class has onCreate and onResume methods that are pretty analagous to viewDidLoad and viewDidAppear.

Activity.onResume

EDIT

To add to this, since some have mentioned in the comments that the view tree is not yet fully available during these callbacks, there is the ViewTreeObserver that you can listen to if you need first access to the view hierarchy. Here is a sample of how you can use the ViewTreeObserver to achieve this.

    View someView = findViewById(R.id.someView);
    final ViewTreeObserver obs = someView.getViewTreeObserver();
    obs.addOnPreDrawListener(new OnPreDrawListener() {

        public boolean onPreDraw() {
            obs.removeOnPreDrawListener(this);
            doMyCustomLogic();
            return true;
        }
    });
Sadoff answered 18/8, 2010 at 15:10 Comment(4)
onCreate is not like viewDidLoad, because in viewDidLoad all views from the xml/xib is created and accessible, but in onCreate the views are not accessible yet.Hellespont
I found out that findViewById can be used inside onCreate after setContentView is called.Hellespont
@Rich, I think it's more like viewWillAppear, rather than viewDidAppear. onPreDraw means to preparing for drawingSaari
@KuangYuang My point was to mention the ViewTreeObserver and demonstrate how to add listeners. Obviously, anyone who finds this on Google can look at all of the other available options in the online documentation. I think for most cases however, onPreDraw is fine because this is after the measure/layout pass and will allow you to know the dimensions of a view and still give you the option to modify it and force another measure/layout pass.Sadoff
C
18

onResume() is more like viewCouldAppear. :) public void onWindowFocusChanged(boolean) is the closest to viewDidAppear. At this point within the activity lifecycle you may ask the view about its size.

Cool answered 10/4, 2012 at 14:50 Comment(0)
G
4

From my limited, nascent understanding of Android, you implement viewDidLoad type functionality in the onCreate method of your Activity:

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

The equivalent for viewDidAppear is closer to the onResume method:

Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(), for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.

Glucoside answered 18/8, 2010 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.