【问题标题】:Custom view onDraw() does not get called again after activity.recreate()在 activity.recreate() 之后不会再次调用自定义视图 onDraw()
【发布时间】:2016-03-22 13:37:40
【问题描述】:

我有一个片段,它有一个充满自定义视图的 viewpager。

当从我的设置活动返回时,MainActivity 获取活动结果并(如果语言更改)调用 recreate()。

调试时,我看到自定义视图通过 onAttachedToWindow() 和 onDetachedToWindow() 并最终在 onAttachedToWindow() 结束,但它们再也不会调用 onDraw(Canvas c),即使我在视图寻呼机上调用 invalidate()或自定义视图本身,因此自定义视图不会显示在页面上。

我也试过调用 setRetainInstance(true);在片段的 onCreateView 中。

我还应该尝试什么?

【问题讨论】:

    标签: android android-lifecycle


    【解决方案1】:

    我在活动方面遇到了与您相同的问题,我发现的唯一方法是再次销毁并创建活动:

    public void setLanguage(Context context, String languageToLoad) {
    
        Resources res = context.getResources();
        // Change locale settings in the app.
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        conf.locale = new Locale(languageToLoad.toLowerCase());
        res.updateConfiguration(conf, dm);
        Intent intent = new Intent(getApplicationContext(), MainMenuActivity.class);
        startActivity(intent);
        finish();
    
    }
    

    我知道这不是最好的方法,但我希望它对你有所帮助。

    我发现了这个:

    if (Build.VERSION.SDK_INT >= 11) {
        recreate();
    } else {
        Intent intent = getIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
    
        startActivity(intent);
        overridePendingTransition(0, 0);
    }
    

    这里How do I restart an Android Activity

    还记得从 FragmentManager 中删除您的片段:

    Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
    if(fragment != null)
        getSupportFragmentManager().beginTransaction().remove(fragment).commit();
    

    【讨论】:

    • 不应该 recreate() 自动执行此操作吗?
    • 意图 i = getIntent();结束();开始活动(一);这导致 Activity 回到开始,这正是我想要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 2012-03-03
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多