美文网首页
有多少种Context?如何进程级别复用View

有多少种Context?如何进程级别复用View

作者: SMSM | 来源:发表于2018-01-23 21:51 被阅读44次

    View的inflate过程最耗时,一个包含5个子view的也要耗时30ms左右。

    有多少种Context

        Activity.getApplicationContext();   返回进程Context
        Activity.getBaseContext();  返回ContextImpl实例
        View.getContext();  对应Activity,Activity是对ContextImpl的代理
    

    从ActivityTrhead中Application的Context

    Context
    ActivityTrhead
    Context appContext = createBaseContextForActivity(r, activity); ---- ContextImpl

    public class ContextWrapper extends Context
    protected void attachBaseContext(Context base)
    public class ContextThemeWrapper extends ContextWrapper
    public class Activity extends ContextThemeWrapper
    final void attach(Context context, ActivityThread aThread,) 对 ContextImpl 做了一次代理
    mWindow = new PhoneWindow(this, window);
    public PhoneWindow(Context context, Window preservedWindow)

    getBaseContext() 获取的是 ContextImpl ,而 ViewGroup中获取Context是容器对应的Activity

    PhoneWindow 是对 进程级别的Window的代理

    如何进程级别复用View

    inflate的过程执行对主线程的检测

    E/AndroidRuntime: FATAL EXCEPTION: Thread-3
                      Process: com.pitaya.loadjar, PID: 11903
                      android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                          at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6891)
                          at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1048)
                          at android.view.View.requestLayout(View.java:19781)
                          at android.view.View.requestLayout(View.java:19781)
                          at android.view.View.requestLayout(View.java:19781)
                          at android.view.View.requestLayout(View.java:19781)
                          at android.view.View.requestLayout(View.java:19781)
                          at android.view.ViewGroup.removeAllViews(ViewGroup.java:4857)
                          at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:278)
                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
                          at com.pitaya.loadjar.MainActivity$1.run(MainActivity.java:55)
                          at java.lang.Thread.run(Thread.java:761)
    
    
        void checkThread() {
            if (mThread != Thread.currentThread()) {
                throw new CalledFromWrongThreadException(
                        "Only the original thread that created a view hierarchy can touch its views.");
            }
        }
    

    已经关联了parent

    同一个View被添加两次,存在 『已经有parent了报错』

    01-23 07:41:27.736 5167-5184/? D/zxx: 187ms 耗时
    01-23 06:18:56.392 15787-16013/com.pitaya.loadjar D/zxx: 0ms 耗时
    反射的方式 清除 parent 
    
    /AndroidRuntime: FATAL EXCEPTION: main
                     Process: com.pitaya.loadjar, PID: 2452
                     java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                         at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
                         at android.view.ViewGroup.addView(ViewGroup.java:4258)
                         at android.view.ViewGroup.addView(ViewGroup.java:4198)
                         at android.view.ViewGroup.addView(ViewGroup.java:4171)
                         at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:279)
                         at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
                         at com.pitaya.loadjar.MainActivity$1$1.run(MainActivity.java:53)
                         at android.os.Handler.handleCallback(Handler.java:751)
                         at android.os.Handler.dispatchMessage(Handler.java:95)
                         at android.os.Looper.loop(Looper.java:154)
                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                         at java.lang.reflect.Method.invoke(Native Method)
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    

    已经关联了Context

    发射的方式 替换为 ApplicationContext,解决内存泄漏的问题,复用时才重新加入当前ActivityContext。

        private void clearContext(View root) {
            long old = System.currentTimeMillis();
            processInjectContext(getApplicationContext(), root);
            Log.d("zxx", (System.currentTimeMillis() - old) + "ms 耗时 clearContext过程");
        }
        
        private void processInjectContext(Context context, View root) {
            if (!(root instanceof ViewGroup)) {
                injectContext(context, root);
                return;
            }
    
            if (root instanceof ViewGroup) {
                ViewGroup viewGroupRoot = (ViewGroup) root;
                for (int i = 0; i < viewGroupRoot.getChildCount(); i++) {
                    clearContext(viewGroupRoot.getChildAt(i));
                }
                injectContext(context, viewGroupRoot);
            }
        }
    
        private void injectContext(Context context, View view) {
            try {
                Field mParent = View.class.getDeclaredField("mContext");
                mParent.setAccessible(true);
                mParent.set(view, context);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    

    相关文章

      网友评论

          本文标题:有多少种Context?如何进程级别复用View

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