美文网首页
获取栈顶Activity

获取栈顶Activity

作者: 毛毛虫撤回一条消息 | 来源:发表于2018-09-04 18:05 被阅读11次
    private void initGlobeActivity() {
    
    /**
    
    * 获取栈顶activity
    
    */
    
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
    
    @Override
    
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    
    }
    
    @Override
    
            public void onActivityDestroyed(Activity activity) {
    
    }
    
    @Override
    
            public void onActivityStarted(Activity activity) {
    
    }
    
    @Override
    
            public void onActivityResumed(Activity activity) {
    
    MyActivityManager.getInstance().setCurrentActivity(activity);
    
            }
    
    @Override
    
            public void onActivityPaused(Activity activity) {
    
    }
    
    @Override
    
            public void onActivityStopped(Activity activity) {
    
    }
    
    @Override
    
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    
    }
    
    });
    
    }
    
    public class MyActivityManager {
    
    private static MyActivityManagersInstance =new MyActivityManager();
    
        private WeakReferencesCurrentActivityWeakRef;
    
        private MyActivityManager() {
    
    }
    
    public static MyActivityManagergetInstance() {
    
    return sInstance;
    
        }
    
    public ActivitygetCurrentActivity() {
    
    Activity currentActivity =null;
    
            if (sCurrentActivityWeakRef !=null) {
    
    currentActivity =sCurrentActivityWeakRef.get();
    
            }
    
    return currentActivity;
    
        }
    
    public void setCurrentActivity(Activity activity) {
    
    sCurrentActivityWeakRef =new WeakReference(activity);
    
        }
    
    }
    

    调用 MyActivityManager.getInstance().getCurrentActivity();

    相关文章

      网友评论

          本文标题:获取栈顶Activity

          本文链接:https://www.haomeiwen.com/subject/carnwftx.html