0

I am showing a countdown while Realm dataBase is loaded from the asset file

 SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
            boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
            if (isFirstRun)
            {
                handler = new Handler();
                Runnable r = new Runnable() {
                    public void run() {
                        Intent intent=new Intent(context,FirstRun.class);
                        startActivity(intent);
                    }
                };
                handler.post(r);


                RealmConfiguration config = new RealmConfiguration.Builder(context)
                        .name(Realm.DEFAULT_REALM_NAME)
                        .migration(new in.webic.longevity.longevity.Word())
                        .assetFile(context, "Default.realm")
                        .schemaVersion(0)
                        .build();

                realm = realm.getInstance(config);
                realm.close();

                SharedPreferences.Editor editor = wmbPreference.edit();
                editor.putBoolean("FIRSTRUN", false);
                editor.commit();

            }

Problem:

countdown should be show, rather a blank screen while asset file is copying as default database

Yet activity starts after few seconds(which is in the thread) taken by the code below to load the asset file,
Is there a better way to show countdown while Realm configuration is setup.
any help would be appreciated
Thanks

phpdroid
  • 1,642
  • 1
  • 18
  • 36

3 Answers3

1

If you want to show a countdown, you must to copy the file manually.

And then, run the slow code in AsyncTask.

Like this:

public class LauncherActivity extends Activity {

     public void onCreate(...) {
        // init view

        new SlowTask().execute();
     }


     class SlowTask extends AsyncTask<Void, Void, Void>{

          protected Void doInBackground(Void... params) {

              // realm slow code here
              return null;
          }

          protected void onPostExecute(Void result) {
             // start your next activity here
          }
     }
}
0

You can't estimate the processing time. So countdown would not be a good option. Show some progress dialog before loading data from assets, then after data is loaded, dismiss the dialog.

Update: Actually what your problem is your thread executes before data is loaded! fine?. What you need to do is to do all processing task in AsyncTask class in which you should show progress dialog in onPreExecute(...) method. then do processing work in doInBackground(...) method. then in onPostExecute(...), dismiss progress dialog.

abdul khan
  • 843
  • 2
  • 7
  • 24
  • i agree, but my question run a thread before the below statement i.e default configuration is loaded – phpdroid Oct 03 '16 at 11:52
0
  1. If you run some time-consuming task in main thread ("UI-thread", thread executed your Activity lifecycle methods) of your application, your app UI will be "freezed", which is generally undesirable. All background tasks (file\network io, heavy calculations) should be executed in separate thread.
    If the background operation is started only for showing something in UI, consider using Loaders
  2. If you need your object to be accessible from several Activites\Fragments, consider extending Application class and place link to your object there. If you need to "initialize" your object (load db in your case) at every app start, this can be done in onCreate() method of your Application class. If some objects (Activities) need to know, when the initialization is finished, you can use Observer pattern.
    Sample code:
    Your Application class:

    public void onCreate() {   
        super.onCreate(); 
        mHandler = new Handler(); //for communicating with UI thread
        if (firstRun) { 
            new Thread(new Runnable() { 
                loadMyDb(); 
                mHandler.post(new Runnable()) { 
                    notifyListeners(); 
                } 
            } 
        } 
    }
    

Your Activity class:

protected void onCreate() {
    super.onCreate();
    setContentView(R.layout.your_layout); 
    //setup your layout and views
    MyApplication app = (MyApplication)getApplicationContext();
    if (app.isDbLoaded()) {
        showContent();
    } else {
        showProgressBar();
        app.addDbLoadListener(this);
    }
 }

 public void onDbLoaded() {
     hideProgressBar();
     app.removeDbLoadListener(this);
     showContent();
 }
stknazev
  • 1
  • 1
  • whole application depends on database which needs to be loaded first hence, code needs to be run synchronously only. so i am starting a new activity to show something and first activity will load the heavy task – phpdroid Oct 03 '16 at 11:58