Issue
I'm trying to add an AsyncTask in my Splash Activity - the AsyncTask doInBackground()
will download DB info (JSON) from Firebase , and will update the DB.
I'm trying to use ProgressDialog
until the data is updated and the Splash Activity goes to the next activity. The ProgressDialog
doesn't appear if I dismiss it on the onPostExecute()
.
It appears if I remove the dismiss()
action, but I can see that the ProgressDialog
freezes. Clearly, something isn't right with my code.
I'm very new in Android development. Please help.
Attached code:
public class NationalSplashActivity2 extends Activity {
NationalDatabaseHelper NationalDB;
xDatabaseHelper xDB;
Cursor cursor;
String default_league;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.national_activity_splash);
final ImageView iv = (ImageView) findViewById(R.id.nationalimageViewSplashScreen);
BackgroundTask backgroundtask = new BackgroundTask(NationalSplashActivity2.this);
backgroundtask.execute();
Thread startTimer = new Thread(){
public void run(){
sleep(2000);
}catch (InterruptedException e){
e.printStackTrace();
}
};
startTimer.start();
}
class BackgroundTask extends AsyncTask<String,String,String> {
Context ctx;
NationalDatabaseHelper NationalDB;
xDatabaseHelper SecDB;
BackgroundTask (Context ctx){
this.ctx = ctx;
mProgressDialog = new ProgressDialog(ctx);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
}
@Override
protected void onPreExecute() {
//super.onPreExecute();
mProgressDialog.show();
}
@Override
protected String doInBackground(String... params) {
NationalAllGamesUpdate();
xAllGamesUpdate();
return "";
}
@Override
protected void onProgressUpdate(String... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String result) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}
Solution
What are you doing here is very weird.. By default, retrieving data from firebase is an asychronous operations, so you don't need to create an async. Just download data, save in database and after that start new activity. As i can see, you create an async to get data and save to database and start a new thread with sleep, interrogate database and after that your are starting new Activity (correct me if I'm wrong).
This is wrong, because you don't know exactly when retrieving process and saving in database is done and your thread is string to access database after some sleep time, where database can be locked by saving process or empty because retrieving is not done. You can start activity right after saving process in database is done and you can use data from firebase response to do that. It can save more time and memory. Progress dialog can be shown when retrieving process is started and need to be dismissed just before starting new activity to prevent memory leaks.
Edit: it seems that you have edited your question.
Edit again:
So you can create an interface that could be passed in database helper constructor, kind of:
new NationalDatabaseHelper(new MyCustomInterface() {})
where MyCustomInterface can have one method or more, depends what you want to notify. I supose it could have method void onTaskCompleted()
or whatever and this method you call after insertion/delete or update method is end because those operations are synchronous, so at the end you should call listener.onTaskCompleted()
and where you implement that interface should call what you want to do next.
The other method, that I recommend, kind of
NationalDatabaseHelper.addEntry(new MyCustomInterface(){});
In this case is more useful because you know what database operation was completed.
You can do whatever you want and is best for you. Hope you understand.
Answered By - Cătălin Florescu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.