Android Lifecycle

完整全面的生命周期鸟瞰图

Fragment/Activity lifecycyle

Activity的生命周期

Activity实现的接口Window.CallBack中的方法

    /**
     * This hook is called whenever the window focus changes.  See
     * {@link View#onWindowFocusChanged(boolean)
     * View.onWindowFocusChangedNotLocked(boolean)} for more information.
     *
     * @param hasFocus Whether the window now has focus.
     */
    public void onWindowFocusChanged(boolean hasFocus);

    /**
     * Called when the window has been attached to the window manager.
     * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
     * for more information.
     */
    public void onAttachedToWindow();

    /**
     * Called when the window has been attached to the window manager.
     * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
     * for more information.
     */
    public void onDetachedFromWindow();

应用

设置对话框样式的Activity

实现思路:

  • 设置Activity的Theme继承“android:Theme.DeviceDefault.Dialog”以实现最基本的对话框样式
  • 在onAttachedToWindow中获取Window对象的成员Decor对象的WindowManager.LayoutParams
public final class ActivityThread {  
    ......  
  
    final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {  
        ......  
  
        ActivityClientRecord r = performResumeActivity(token, clearHide);  
  
        if (r != null) {  
            final Activity a = r.activity;  
            ......  
  
            // If the window hasn't yet been added to the window manager,  
            // and this guy didn't finish itself or start another activity,  
            // then go ahead and add the window.  
            boolean willBeVisible = !a.mStartedActivity;  
            if (!willBeVisible) {  
                try {  
                    willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(  
                            a.getActivityToken());  
                } catch (RemoteException e) {  
                }  
            }  
            if (r.window == null && !a.mFinished && willBeVisible) {  
                r.window = r.activity.getWindow();  
                View decor = r.window.getDecorView();  
                decor.setVisibility(View.INVISIBLE);  
                ViewManager wm = a.getWindowManager();  
                WindowManager.LayoutParams l = r.window.getAttributes();  
                a.mDecor = decor;  
                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;  
                ......  
                if (a.mVisibleFromClient) {  
                    a.mWindowAdded = true;  
                    wm.addView(decor, l);  
                }  
            }   
  
            ......  
        }  
  
        ......  
    }  
    
    ......  
}  

从源码中可以看到Decor的LayoutParam实在Activity执行完onResume()之后设置。在这个生命周期里面设置Activity的DecorView的宽高,然后使用shape资源设置背景

参考

实现圆角对话框样式的Activity

你可能感兴趣的:(Android Lifecycle)