Android 更好的Activity生命周期回调-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

Android 更好的Activity生命周期回调

简介: /** * This class allows you to listen to when the user is entering the background (i.e. after a home button press, * or opening recent apps etc)...
/**
 * This class allows you to listen to when the user is entering the background (i.e. after a home button press,
 * or opening recent apps etc) and when the user resumes the application from the background.
 * 
 * @author John McDonnell
 */
public class BetterActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
 
 
    private int mForegroundActivities;
    private boolean mHasSeenFirstActivity;
    private boolean mChangingConfiguration;
 
    @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
 
    }
 
    @Override public void onActivityStarted(Activity activity) {
        mForegroundActivities++;
 
        if (mHasSeenFirstActivity && mForegroundActivities == 1 && !mChangingConfiguration) {
            applicationDidEnterForeground(activity);
        }
 
        mHasSeenFirstActivity = true;
        mChangingConfiguration = false;
    }
 
    @Override public void onActivityResumed(Activity activity) {
 
    }
 
    @Override public void onActivityPaused(Activity activity) {
 
    }
 
    @Override public void onActivityStopped(Activity activity) {
        mForegroundActivities--;
 
        if (mForegroundActivities == 0) {
            applicationDidEnterBackground(activity);
        }
 
        mChangingConfiguration = activity.isChangingConfigurations();
    }
 
    @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
 
    }
 
    @Override public void onActivityDestroyed(Activity activity) {
 
    }
 
    /**
     * One day we'll be as cool as iOS
     */
    protected void applicationDidEnterForeground(Activity topActivity) {
 
    }
 
    protected void applicationDidEnterBackground(Activity lastActivity) {
 
    }
 
}

  

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: