5

I have an activity 'Activity1' and also another activity named 'Activity2'. The 'Activity2' is loaded upon clicking a button in 'Activity1'. I wanted to display the progress dialog until the new activity is loaded . Can you please show me the code to do this

Ashish Augustine
  • 1,784
  • 4
  • 20
  • 50

5 Answers5

4

Display the progress dialog in Activity2's onCreate method, then do all the time-consuming loading in an AsyncTask. In the AsyncTask's onPostExecute() method, dismiss the progress dialog.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
2

yes by using AsynTask<> you can get your result

in OnPreExecute Show your Progress dialog,in OndoInBackground run your activity,in onPostExecute remove your dialog

get the idea Get Concept

Community
  • 1
  • 1
Last Warrior
  • 1,307
  • 1
  • 11
  • 20
2

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.
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
1

The other answers (using AsynTask) are correct, but the question you need to figure out is what is causing your delay. Is it something happening on the back end of Activity1 or something happening on the front end Activity2. If you're doing some processing before starting Activity2 then follow the advice of Last Warrior or Ted Hopp... if you have some lengthy loading process in Activity2 then you'll need to initiate the progress dialog as the first thing that happens onCreate of Activity2 and then move whatever is taking up processing resources off into an AsynTask (or just another thread) there.

I guess in the unlikely event that both A1 and A2 are requiring extra time on the end and front of each respectively, you'll need to open and close a progress dialog... I don't think there's a way to keep one open in the foreground as you move from one activity to the other.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
1

you can do it through AsyncTAsk. Which code taking time for executing just put that into

doInBackground()

override method of asyncTask and

startActivity(intent) ----just put into onPostExcute()

  protected class InitTask extends AsyncTask<Context, Integer, Integer> {

  @Override protected Integer onPreExcute( Context... params )  {

        //assign progressbar here

}
    @Override protected Integer doInBackground( Context... params )  {

         //do all the stuffs here
         return super.doInBackground( params )

} @Override protected void onProgressUpdate(Integer... values)  {
         super.onProgressUpdate(values); 
         //update progress bar
}

    @Override protected void onPostExecute( Integer result )  {
          super.onPostExecute(result);
          //start activity here
 }

}
Jyosna
  • 4,436
  • 13
  • 60
  • 93