2

I have a button in one page and when I click on that button I am able to go another activity through Intent(), but onbuttonclick() in which activity I am going in that activity data in spinner coming from server means on button click I load that data on spinner from server.so it takes times for moving my button click activity to other activity so I want to show progress bar when my button is clicked and untill data is not coming from server...how to achieve this..and I want to show progress bar on buttonclick page means on my first activity when I click the button.

My code of of on button click is given below.

cuurentloc.setOnClickListener(new View.OnClickListener()
        {
      public void onClick(View v) {
      Intent i = new Intent(MainMenu.this, currentlocmap.class);
    startActivity(i);

     }
      });

Actually I know asynchronous task but using this I will be able to show progress bar on 2nd activity, I want to show it on my first activity until data is not loaded in second activity, so I want progree bar above the button on first activity, and when data is loaded on second activity it moves to second.

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
SRam
  • 2,832
  • 4
  • 45
  • 72

4 Answers4

4

You need to use AsyncTask as the way I am guiding here. Create Async Task in first activity. On button click event call that AsyncTask. In background do loading data from server. and onPostExecute start second activity

cuurentloc.setOnClickListener(new View.OnClickListener()
        {
      public void onClick(View v) {
      new ProgressTask(MyClassName.class).execute(null);

     }
      });

Async Task

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;
        List<Message> titles;
        private ListActivity activity;
        //private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }



        /** progress dialog to show user that the backup is processing. */

        /** application context. */
        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }

            @Override
        protected void onPostExecute(final Boolean success) {

                if (dialog.isShowing()) {
                dialog.dismiss();
            }

            Intent i = new Intent(MainMenu.this, currentlocmap.class);
    startActivity(i);

        }

        protected Boolean doInBackground(final String... args) {
            try{    
                //load data from server
             } catch (Exception e){
                Log.e("tag", "error", e);
                return false;
             }
          }


    }

}

Thanks Deepak

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • pass this one. new ProgressTask(context).execute(null); Have a look on my code. I have resolved your issue now – Sunil Kumar Sahoo May 30 '11 at 06:33
  • @Deepak hi..thanks again ..next if i had data in array list after operation performed in doInBackground() then how it will be send through intent to other activity ..can i use putExtras() method for sending arraylist data if yes then how to achive this... – SRam May 30 '11 at 07:02
  • Use i.putStringArrayListExtra("data", ArrayList); – Sunil Kumar Sahoo May 30 '11 at 07:15
  • can i make static type of array list in first activity and use it in second activity??is it possible or not??or any draw back???means static ArrayList ciudad1 = new ArrayList(); – SRam May 30 '11 at 07:18
  • While calling use FistActivityname.ciudad – Sunil Kumar Sahoo May 30 '11 at 07:26
1

have a look at this code

package com.exercise.AndroidBackgroundThread;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidBackgroundThread extends Activity {

Thread backgroundThread;
TextView myText;
boolean myTextOn = true;
boolean running = false;

Handler handler = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
if (myTextOn){
myTextOn = false;
myText.setVisibility(View.GONE);
}
else{
myTextOn = true;
myText.setVisibility(View.VISIBLE);
}
}

};

void setRunning(boolean b){
running = b;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myText = (TextView)findViewById(R.id.mytext);

Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();       
}



@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();

Toast.makeText(this, "onStart()", Toast.LENGTH_LONG).show();

backgroundThread = new Thread(new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
while(running){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendMessage(handler.obtainMessage());
}
}

});

setRunning(true);
backgroundThread.start();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();

boolean retry = true;
setRunning(false);

while(retry){
try {
backgroundThread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Toast.makeText(this, "onStop()", Toast.LENGTH_LONG).show();

}
}
and for more detail look at this guide http://tech-aamir.blogspot.in/2012/06/how-to-make-progress-bar-when.html

Best of luck
aamirkhan i.
Aamirkhan
  • 5,746
  • 10
  • 47
  • 74
0

You can use the ProgressBar or ProgressDialog in the currentlocmap class. Use AsyncTask class for that and when the data is fetched, set the layout using setContentView() and dismiss the ProgressDialog.

Refer to these links:

Fetch data from server and refresh UI when data is fetched?

How to start and finish progressBar dynamically in android

Community
  • 1
  • 1
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30
0

To build on the first answer, since you are familiar with AsyncTask. You can have the AsyncTask perform the work to retrieve whatever data you'll need in your first activity. And during that process, you display your progress bar. Once the AsyncTask completes, you remove the progress bar, put your data in a bundle (by calling putExtras), and send it off with your intent to start the 2nd Activity.

feiyingx
  • 329
  • 4
  • 13
  • hi ..if in first activity i have data in an array variable then how it will be sent using putExtras()...pls help me...thanks – SRam May 30 '11 at 06:52
  • @trivedi What type of array data are you trying to pass? the putExtras() method has overloads to pass array data for primitive data types: int[], byte[], float[], String[], boolean[], double[], etc. If you are using a custom class, the putExtras() method also supports Parcelable[] array. Your custom class will have to implement the Parcelable interface. http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, int[]) – feiyingx May 31 '11 at 03:35