Search code examples
androidandroid-progressbarsetcontentview

Android setContentView and computing


The first activity of my application is checking the database and upgrading it if needed.

@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(getThemeFromPreferences(this));
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
}

@Override
public void onResume() {
    super.onResume();
    //Test et remplissage de la BDD
    ProgressBar progressBar = (ProgressBar) findViewById(R.id.firstProgressBar);
    DatabaseFeeder.testAndFeed(this, progressBar);
    //Launch welcoming activity
    Intent i = new Intent(this, AccueilActivity.class);
    startActivity(i);
}

The DatabaseFeeder.testAndFeed static method is a little slow because it reads from assets, so it can take like 5 secs to fill the database.

Problem is : the contentView is not set before the feeder is finished ! My activity is all blank for 5 seconds and then pop as my content view, including fully filled progressbar.

Why is that so ?


Solution

  • run following code in an async task:

     DatabaseFeeder.testAndFeed(this, progressBar);
    

    this will keep applications main thread free for rendering required views. also, it is always a good idea to separate long-running operations from main thread of application.