4

I have created an splash screen, which works perfectly. Now I want to load database while showing splash screen and after database has been loaded completely show the application UI.
I have the following code to do this is this right?

public class Splash extends Activity{

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    new LoadDatabase().execute();
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

// Load DB
protected class LoadDatabase extends AsyncTask<Context, Integer, String> 
{ 
     @Override 
     protected String doInBackground( Context... params ) { 
         try {
            new DatabaseHelper(getApplicationContext()).initializeDatabase();
        } catch (IOException e) {
            e.printStackTrace();
        }
         return "";
     } 

     @Override 
     protected void onPreExecute() { 
        super.onPreExecute(); 
        setContentView(R.layout.splash);
     } 

     @Override
    protected void onPostExecute(String result) {           
        super.onPostExecute(result);
         Intent openMain = new Intent("com.nepways.MAIN"); 
         startActivity(openMain); 
    }
}

}


Is there any good example or suggestions?? please help me out with this.

aman.nepid
  • 2,864
  • 8
  • 40
  • 48
  • I found tutorial for UI Thread and Background [Processing](http://www.vogella.de/articles/AndroidPerformance/article.html#overview_intro). and Refer this [answer](http://stackoverflow.com/questions/1979524/android-splashscreen/1982002#1982002). – Uttam Oct 04 '11 at 08:12

4 Answers4

3

You may perform this operation through async or thread and handler

protected class MyTask extends AsyncTask<Context, Integer, String> 
{ 
     @Override 
     protected String doInBackground( Context... params ) 
     { 
      // write ur database functionality 
     } 

     // -- gets called just before thread begins 
     @Override 
     protected void onPreExecute() 
     { 
        // u may show ur progressbar or spalsh screen
        super.onPreExecute(); 

     } 


     // -- called as soon as doInBackground method completes 
     @Override 
     protected void onPostExecute() 
     { 
       super.onPostExecute(result); 
      // Intent openMain = new Intent("com.nepways.MAIN"); 
            startActivity(openMain); 
     } 
} 

Try this ... may be it will help u

Andy
  • 5,379
  • 7
  • 39
  • 53
2

Invoke an Activity, lets call it SplashScreen. invoke an AsyncTask from this activity that accesses another class created by you, extending SQLiteOpenHelper. This class initializes the database.
When this AsyncTask finishes working, it returns the control to UI thread, now close the SplashScreen.

Aman Alam
  • 11,231
  • 7
  • 46
  • 81
  • Its demo is here in SO. check out : http://stackoverflow.com/questions/2222890/how-to-make-a-splash-screen-screen-visible-when-app-starts – Kartik Domadiya Oct 04 '11 at 07:20
0

load the data base in a thread and once done go to next screen using handler ,Use handler Class for android .search handler and thread you would get lot of solutions.

-1

In your onCreate of SplashScreen write the following code

    myDataBase=this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
    DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());
    dbHelper.openDataBase();

Declare your database details before the constructor

Rashmi.B
  • 1,787
  • 2
  • 18
  • 34
  • This will do the initialization on the UI Thread, which could make your application non responsive if it takes longer than a few seconds. Consider using an AsyncTask instead. – Yohan Liyanage Aug 31 '17 at 08:46