I have an Activity which downloads the data from the Database. While the Activity is doing this work, I want to show the progress with ProgressDialog.I use ProgressDialog.STYLE_HORIZONTAL because I want to show the actual values. I use a Handler to start the Activity which displays the ProgressDialog:
Intent intent = new Intent(this, ProgressDialogActivity.class);
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == SET_PROGRESS){
intent.putExtra("action", "show");
intent.putExtra("progress", msg.arg1);
intent.putExtra("max", msg.arg2);
intent.putExtra("message", syncMessage);
intent.putExtra("title", R.string.please_wait);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else if(msg.what == SHOW_PROGRESS){
intent.putExtra("action", "show");
intent.putExtra("title", syncMessage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else if(msg.what == HIDE_PROGRESS){
intent.putExtra("action", "hide");
intent.putExtra("message", "");
intent.putExtra("title", "");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
};
Here is the ProgressDialogActivity:
public class ScreenProgressDialog extends Activity {
ProgressDialog pd;
Bundle extras;
String action;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
extras = getIntent().getExtras();
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false);
pd.setProgress(extras.getInt("progress"));
pd.setMax(extras.getInt("max"));
pd.setMessage(extras.getCharSequence("message"));
pd.setTitle(extras.getString("title"));
action = extras.getString("action");
if (action.equals("show")){
pd.show();
}
else{
pd.dismiss();
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
When the Main Activity downloads a new table from the Database the Handler starts a new ProgressDialogActivity and a new Activity appears. I would like to avoid this. My aim is to show only ONE Activity which displays the ProgressDialog with the correct values.
(I cannot create a ProgressDialog in the Main Activity, I have to find another way. It’s some kind of homework but I need some help).
Thank you!
You can use AsyncTask to fetch data in background and show ProgressDialog
Here is code: