用户可见活动后执行方法 [英] Execute a method after an activity is visible to user

查看:43
本文介绍了用户可见活动后执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动包含太多UI控件.我想在活动可见后执行一个方法.

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

我尝试过的示例:

public class Main extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
             MyMethod();
    }

    private void MyMethod(){
        Toast.makeText(this, "Hi UI is fully loaded", Toast.LENGTH_SHORT).show();
    }
}

但是在上面的示例中,该消息显示在活动可见之前.

But in the above sample, the message shows before the activity is visible.

有没有办法找出活动是否完全可见?

Is there a way to find out, if the activity is fully visible ?

推荐答案

将代码移至onResume

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

检查活动生命周期

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

protected void onResume ()

在onRestoreInstanceState(Bundle),onRestart()或onPause()之后调用,以使您的活动开始与用户进行交互.这是开始动画,打开专用访问设备(例如相机)等的好地方.

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.

请记住,onResume并不是最好的指示,表明用户可以看到您的活动;系统窗口(例如键盘锁)可能在前面.使用onWindowFocusChanged(boolean)可以确定您的活动对用户可见(例如,恢复游戏).

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.

这篇关于用户可见活动后执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆