Execute a method after an activity is visible to user

前端 未结 5 858
无人及你
无人及你 2021-01-31 18:04

I have an activity contains too many UI controls. I want to execute a method after make the activity visible.

An example i tried:

publi         


        
5条回答
  •  花落未央
    2021-01-31 18:59

    Move your code to onResume

    @Override
    protected void onResume()
    {
        super.onResume();
        MyMethod();
    }
    

    Check the activity lifecycle

    http://developer.android.com/reference/android/app/Activity.html

    protected void onResume ()
    

    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.

    Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

    Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

提交回复
热议问题