Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying progress dialog before starting a new activity

Tags:

android

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

like image 512
Ashish Augustine Avatar asked Dec 01 '11 05:12

Ashish Augustine


People also ask

What is progress dialog?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

What can I use instead of progress dialog?

ProgressBar is best alternative for ProgressDialog.


3 Answers

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.

like image 154
Ted Hopp Avatar answered Sep 22 '22 07:09

Ted Hopp


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.
like image 36
Sunil Kumar Sahoo Avatar answered Sep 21 '22 07:09

Sunil Kumar Sahoo


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

like image 37
Last Warrior Avatar answered Sep 23 '22 07:09

Last Warrior