简体   繁体   中英

Android displaying white screen until Asynctask starts but I have already loaded a layout

So I have the following code and a problem: It should load a layout that should say "downloading database..." (dynamically added to the layout) however I get a white-screen until the database is updated and then it just proceeds with add_categories_layout(0) and works fine.

How do I get it to show me the first layout?

protected void onResume(){

    super.onResume();
    setContentView(R.layout.home_screen_layout);
    SF = (LinearLayout) findViewById(R.id.SF);
    p_db_adapter = new DBAdapterProducts(getApplicationContext());
    c_db_adapter = new DBAdapterCategories(getApplicationContext());
    add_downloading_DB_layout();

    new UpdateDB().execute(CATEGORY_URL, PRODUCTS_URL, TIMESTAMP_URL_CATEGORIES, TIMESTAMP_URL_PRODUCTS);       
    while(DBupdated == false){}
    add_categories_layout(0);

}

You should be putting setContentView in either onCreate() or onCreateView()

Also can we see the dynamic adding of the layout?

在onResume完成之前,它不会绘制屏幕,​​而在DBUpdated为true之前,onResume将不会完成(此时交换布局)。

Typically you set the contentview in the oncreate() method. There may be code in there causing it not no work. If you dont have on Oncreate method, make one and copy/paste this code in there.

You can put add_categories_layout(0); in the onPostExecute() of your AsyncTask since it runs on the UI Thread . So when your task finishes it will run this method and onResume() can finish doing what it needs. If your AsyncTask is an inner class then you will have access to the Activity methods and member variables. If it is a separate file then you just need to pass a reference of your Activity to it to call the method.

Here are a couple answers that can help with it if you need.

As others have said, typically setContentView() is done in onCreate() to ensure it is one of the first things that runs but this shouldn't be a problem. However, I would move it there unless you have a reason to have it in onResume() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM