I have a main activity that launches a child activity using the following code:
Intent intent = new Intent();
intent.setClassName(MyChildActivity.class.getPackage().getName(), MyChildActivity.class.getName());
((Activity)context).startActivity(intent);
I am trying to perform a time-consuming task in the child activity and would like to display a ProgressDialog while I do so. My code looks like this:
private ProgressDialog _progressDialog;
private OnClickListener btn_onClick = new OnClickListener() {
public void onClick(View v) {
_progressDialog = ProgressDialog.show(
v.getContext(),
"Please wait",
"Performing task..."
);
TaskThread t = new ExportThread(v.getContext());
t.start();
}
};
private class TaskThread extends Thread{
private Context _context;
public TaskThread(Context context) {
_context = context;
}
private Handler _handler = new Handler() {
@Override
public void handleMessage(Message msg) {
_progressDialog.dismiss();
}
};
@Override
public void run() {
performTask(_context);
_handler.sendEmptyMessage(0);
}
}
For some reason, the ProgressDialog is not displaying. If I use that same code in the main activity, it works - but not in the child activity. In addition, the following code also fails to display the ProgressDialog (but the Toast does display):
private ProgressDialog _progressDialog;
private OnClickListener _btn_onClick = new OnClickListener() {
public void onClick(View v) {
_progressDialog = ProgressDialog.show(
v.getContext(),
"Please wait",
"Performing task..."
);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_progressDialog.dismiss();
Toast.makeText(v.getContext(), "Done with progress dialog.", Toast.LENGTH_SHORT).show();
}
};
Any ideas out there? Are we not allowed to display a ProgressDialog from a child activity?
Thank you.