How to Display Progress Dialog Before Starting an Activity in Android

How to display progress dialog before the app or activity loads?

ok if you want show a dialog for 1 or 2 seconds you can use Handler class.like

 ProgressDialog dialog=new ProgressDialog(StartUpActivity.this);
dialog.setMessage("Welcome to Mea Vita, please wait till the app loads.");
dialog.setCancelable(false);
dialog.setInverseBackgroundForced(false);
dialog.show();

this is as your ProgressDialog
write this code for the delay of 1 second or 2 seconds.

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Here you can send the extras.
Intent i = new Intent(this, NextActivity.class);
startActivity(i);

// close this activity
finish();
}
}, 2000);

How to display progress dialog before starting an activity in Android?

You should load data in an AsyncTask and update your interface when the data finishes loading.

You could even start a new activity in your AsyncTask's onPostExecute() method.

More specifically, you will need a new class that extends AsyncTask:

public class MyTask extends AsyncTask<Void, Void, Void> {
public MyTask(ProgressDialog progress) {
this.progress = progress;
}

public void onPreExecute() {
progress.show();
}

public void doInBackground(Void... unused) {
... do your loading here ...
}

public void onPostExecute(Void unused) {
progress.dismiss();
}
}

Then in your activity you would do:

ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new MyTask(progress).execute();

How to show ProgressDialog across launching a new Activity?

You can show a progress dialog like this -

Define this

private ProgressDialog pd = null;

in your activity class

Put this in your onCreate (Dont setContentView directly here)

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.pd = ProgressDialog.show(this, "Fancy App",
"Loading...Please wait...", true, false);
// Start a new thread that will download all the data
new IAmABackgroundTask().execute();

}

// Background heavy lifting

class IAmABackgroundTask extends
AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
// showDialog(AUTHORIZING_DIALOG);
}

@Override
protected void onPostExecute(Boolean result) {

// Pass the result data back to the main activity
ActivityName.this.data = result;

if (ActivityName.this.pd != null) {
ActivityName.this.pd.dismiss();
}

setContentView(R.layout.main);

}

@Override
protected Boolean doInBackground(String... params) {

//Do all your slow tasks here but dont set anything on UI
//ALL ui activities on the main thread

return true;

}

}

Also go through this :http://developer.android.com/training/improving-layouts/index.html for optimizing layout performance.
Also Use Traceview to look for bottlenecks

Displaying progress dialog before starting a new activity

There is two ways to

First approach To use Async Task

If you are doing heavy tasks eg loading data from server or parsing xml in that case use AsynTask<>
If you want to call ActivityB from ActivityA then

*step-1*create a AsyncTask class. write all background tasks inside doBackground() method and after completion of task you want to call an activity that code write inside onPostExecute() post execute method

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;

public class LoadingDataFromServer extends AsyncTask {
Context currentContext = null;

boolean isCancelled = false;

public LoadingDataFromServer(Context context) {
currentContext = context;

}

@Override
protected void onPreExecute() {
if (DashboardActivity.progressBarLayout != null) {
DashboardActivity.progressBarLayout.setVisibility(View.VISIBLE);
// Log.i(TAG,".....Now make progress bar visible.....");
}

super.onPreExecute();
}

@Override
protected Object doInBackground(Object... params) {
// do background processing

try {
// do background tasks eg sever communication
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
// progressDialog.dismiss();

// call second Activity
Intent i = new Intent(currentContext, com.ActvityB.class);
super.onPostExecute(result);
}

@Override
protected void onCancelled() {
// TODO Auto-generated method stub
isCancelled = true;
super.onCancelled();
}

}

step-2 In the activity fro where you want to jump to new activity (eg in ActivityA) call the execute() of AsynTask

new LoadingDataFromServer(context).execute(null);

Second approach

  • First show progress dialog.
  • create a thread to do all background tasks. when the thread completes
    the task then cancel the progress dialog and call the next activity

or

  • when thread complets the task then call next activity pass this
    object (progress dialog) and inside that new activity dismiss this
    dialog.

Progress Dialog on open activity

I had the same issue and using an AsyncTask is working for me.

There are 3 important methods to override in AsyncTask.

  1. doInBackground : this is where the meat of your background
    processing will occur.
  2. onPreExecute : show your ProgressDialog here ( showDialog )
  3. onPostExecute : hide your ProgressDialog here ( removeDialog or dismissDialog
    )

If you make your AsyncTask subclass as an inner class of your activity, then you can call the framework methods showDialog, dismissDialog, and removeDialog from within your AsyncActivity.

Here's a sample implementation of AsyncTask:

class LoginProgressTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
try {
Thread.sleep(4000); // Do your real work here
} catch (InterruptedException e) {
e.printStackTrace();
}
return Boolean.TRUE; // Return your real result here
}
@Override
protected void onPreExecute() {
showDialog(AUTHORIZING_DIALOG);
}
@Override
protected void onPostExecute(Boolean result) {
// result is the value returned from doInBackground
removeDialog(AUTHORIZING_DIALOG);
Intent i = new Intent(HelloAndroid.this, LandingActivity.class);
startActivity(i);
}
}

Android - how to show progress dialog while service is running?

create broadcast messages when you want to show or hide progress bar from your service:

Intent i = new Intent("yourPackage.SHOW_Progress");
sendBroadcast(i);

then create broadcast receiver and handle received messages:

public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
MainActivity mainActivity = ((MyApplication) context.getApplicationContext()).mainActivity;
mainActivity.showProgress();
}

}

and inside your mainActivity create method for show or hide progress bar



Related Topics



Leave a reply



Submit