Reputation: 1735
I need to show a busy indicator in a number of different activities while data is being loaded. It's not a difficult problem, but I hate repeating myself. A base class isn't an option because I don't always extend the same base activity.
protected void updateProgressDialog() {
//we're going to keep the progress dialog around if anything's busy
if (getBusy()) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this, R.style.ProgressDialog);
mProgressDialog.show();
mProgressDialog.setContentView(R.layout.progress_layout);
}
} else {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
}
Upvotes: 0
Views: 333
Reputation: 2324
Above given answers are pretty clear and correct. But in case if you do not want to extend all your classes to BaseActivity then you can select other approach. Here it is :-
Here is code snippest.
MyApplication.java
public class MyApplication extends Application {
static ProgressDialog progressDialog;
void showDialog(String title, String message) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(getApplicationContext());
}
progressDialog.setTitle(title);
progressDialog.setMessage(message);
progressDialog.show();
}
void dismissDialog() {
progressDialog.dismiss();
}
}
Manifest File (We just applied name property.)
<application android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"/>
MainActivity (Activity where we will use showDialog and dismissDialog.)
...
((MyApplication) getApplication()).showDialog("Hi", "This is message.");
((MyApplication) getApplication()).dismissDialog();
...
Here you don't need to edit your Activities Signature. You just create SubClass of Application class that's it.
Hope this helps !
Thanks.
Upvotes: 3
Reputation: 1
It seems to be a design problem of your app: long time processes should run in background thread instead if main thread. Activity's methods called in main (in other name UI) thread and it hangs up the UI if you make a long time process.
If you want to notify the user about the long time background task status you can use:
Asynctask example:
public interface UserNotificationService {
void showProgressBar();
void hideProgressBar();
}
public class UserNotificationServiceImpl implements UserNotificationService {
private Context ctx;
private ProgressDialog mProgressDialog;
public UserNotificationServiceImpl(Context ctx) {
this.ctx = ctx;
}
@Override
public void showProgressBar() {
if(mProgressDialog == null) {
mProgressDialog = new ProgressDialog(ctx, R.style.ProgressDialog);
mProgressDialog.show();
mProgressDialog.setContentView(R.layout.progress_layout);
}
}
@Override
public void hideProgressBar() {
if(mProgressDialog!=null) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
}
background task:
public class ExampleTask extends AsyncTask<Void, Void, Void> {
private UserNotificationService notificationService;
public ExampleTask(Context ctx) {
notificationService = new UserNotificationServiceImpl(ctx);
}
@Override
protected Void doInBackground(URL... urls) {
// ... do your long time work here ...
}
@Override
protected void onPreExecute () {
notificationService.showProgressBar();
}
@Override
protected void onPostExecute(Void result) {
notificationService.hideProgressBar();
}
}
Upvotes: 0
Reputation: 109257
First, make a BaseActivity put updateProgressDialog()
method in it. Now, extends your Activity with this BaseActivity.
public class BaseActivity extends Activity {
...
ProgressDialog mProgressDialog ;
protected void updateProgressDialog() {
//we're going to keep the progress dialog around if anything's busy
if (getBusy()) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this, R.style.ProgressDialog);
mProgressDialog.show();
mProgressDialog.setContentView(R.layout.progress_layout);
}
} else {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
}
...
}
Now your other Activity like,
public class MyActivity extends BaseActivity {
...
getBusy()
{
.. getBusy() work done
}
...
}
Because I assumed getBusy()
methods are available in in your normal activity.
Second, Put it in any Java class and make updateProgressDialog(Context context, boolean flag)
with activity context and boolean paamters
ProgressDialog mProgressDialog ;
public void updateProgressDialog(Context context, boolean flag) {
//we're going to keep the progress dialog around if anything's busy
if (flag) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(context, R.style.ProgressDialog);
mProgressDialog.show();
mProgressDialog.setContentView(R.layout.progress_layout);
}
} else {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
}
Now from your activity call it like,
<Class_Object>.updateProgressDialog(this, getBusy()); //Here this refers to Activity reference
Third, It same as Second but instead of Normal Java Class you have to put your method in Android Application Class which has Context own it. So no need to pass Context, only second boolean Argument, Thats it.
And call it like, (From your activity)
((<Your_Application_Class>)getApplicationContext()).updateProgressDialog(getBusy());
Upvotes: 1
Reputation: 1502
In public static class
public static void updateProgressDialog(Context context, ProgressDialog mProgressDialog) {
//If you can't access getBusy(), you may want to use a boolean argument here
if (getBusy()) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(context, R.style.ProgressDialog); //you might have to cast context, not sure
mProgressDialog.show();
mProgressDialog.setContentView(R.layout.progress_layout);
}
} else {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
}
The other answer is better practice tho.
Upvotes: 0