的setContentView执行过程中黑屏

的setContentView执行过程中黑屏

本文介绍了的setContentView执行过程中黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MainActivity 。有时,当它加载我看到黑色画面一秒钟。我测定时操作的的onCreate 的方法,发现一秒钟以上,被废为的setContentView(R.layout.main_screen); 我preFER这个黑色屏幕,显示previous屏幕(在我的情况下,闪屏),而不是的setContentView 执行过程中。 我如何能摆脱掉这种黑屏?

好像机器人以某种方式preloads布局和这样的问题时有发生。但是,如果我杀了我的进程,并启动应用程序,我总是看到这个黑色的屏幕。

解决方案
  1. 使用一个静态变量来处理查看缓存。
  2. 使用的的AsyncTask 来不冻结你的由来活动
  3. 使用 LayoutInflater 膨胀的查看的布局和缓存它
  4. 的onCreate()目标活动的设置缓存

事情是这样的:

产地活性

  ...
                // noinspection选中
                新的AsyncTask<虚空,虚空,虚空>(){
                    @覆盖
                    保护无效doInBackground(虚空...... PARAMS){
                        LayoutInflater充气=(LayoutInflater)
                                MainParentActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        //非常非常缓慢的动作,如果你的活动是重
                        DialerActivity.cachedView = inflater.inflate(R.layout.dialer_portrait,空,假);
                        返回null;
                    }

                    @覆盖
                    保护无效onPostExecute(虚空避免){
                        意向意图=新的意图(MainParentActivity.this,DialerActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(意向);
                    }
                }。执行();

...
 

目标活动

 公共类DialerActivity扩展MainParentActivity {
    静态视图cachedView = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        如果(cachedView!= NULL){
            的setContentView(cachedView);
        } 其他 {
            的setContentView(R.layout.dialer_portrait);
        }
    }
 。 。 。
 

您也可以使用一个ProgressDialog而膨胀,以避免在过渡冻结的感觉。

I have a MainActivity. Sometimes when it is loading I observe black screen for a second.I measured timings for operations in onCreate method and discovered that more than one second was spent for setContentView(R.layout.main_screen);I prefer to show previous screen (in my case Splash screen) instead of this black screen during setContentView execution. How can I rid off this black screen?

Seems android in some way preloads layouts and such problems occurs sometimes. But if I kill my process and start app I always see this black screen.

解决方案
  1. Use a static variable to handle the View cache.
  2. Use an AsyncTask to don't freeze your origin Activity
  3. Use LayoutInflater to inflate the View layout and cache it
  4. In the onCreate() of the target Activity set the cache

Something like this:

Origin activity

...
                //noinspection unchecked
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        LayoutInflater inflater = (LayoutInflater)
                                MainParentActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        // VERY VERY SLOW action if your Activity is heavy
                        DialerActivity.cachedView = inflater.inflate(R.layout.dialer_portrait, null, false);
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        Intent intent = new Intent(MainParentActivity.this, DialerActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                }.execute();

...

Target activity

public class DialerActivity extends MainParentActivity {
    static View cachedView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (cachedView != null) {
            setContentView(cachedView);
        } else {
            setContentView(R.layout.dialer_portrait);
        }
    }
 . . .

You can use also a ProgressDialog while inflating to avoid freeze sensation on the transition.

这篇关于的setContentView执行过程中黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:36
查看更多