Reputation: 1163
im stuck in a situation where i am switching from activity 1 to activity 2. i am using Thread.sleep(5000) to start another activity after 5 seconds But the progress bar which i want to run for five seconds also sleeps with the first activity Pleaze help me as to when i click next Button on first activity a progress bar shoud run for five sec and then activity should be loaded My Code is:
public class Activity1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.B);
final ProgressBar p=(ProgressBar) findViewById(R.id.pr);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
p.setVisibility(4);
Thread t=new Thread();
try{
t.sleep(5000);
}
catch(Exception e){}
Intent myIntent = new Intent(view.getContext(), activity2.class);
startActivityForResult(myIntent, 0);
}
});
}}
Upvotes: 1
Views: 7979
Reputation: 11316
Change your OnClickListener for this. This will not block your main thread, as you are doing (that explains why your application freezes for 5 seconds):
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new AsyncTask<Integer, Long, Boolean>()
{
ProgressDialog pd;
@Override
protected Boolean doInBackground(Integer... params)
{
pd = new ProgressDialog(Activity1.this);
pd.setTitle("Loading Activity");
pd.setMessage("Please Wait ...");
pd.setMax(params[0]);
pd.setIndeterminate(false);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
publishProgress(0L);
long start = System.currentTimeMillis();
long waitTime = params[0] * 1000;
try
{
while (System.currentTimeMillis() - start < waitTime)
{
Thread.sleep(500);
publishProgress(System.currentTimeMillis() - start);
}
}
catch (Exception e)
{
return false;
}
return true;
}
@Override
protected void onProgressUpdate(Long... values)
{
if (values[0] == 0)
{
pd.show();
}
else
{
pd.setProgress((int) (values[0] / 1000));
}
}
@Override
protected void onPostExecute(Boolean result)
{
pd.dismiss();
Intent myIntent = new Intent(view.getContext(), activity2.class);
startActivityForResult(myIntent, 0);
}
}.execute(5);
});
Upvotes: 5
Reputation: 16570
Don't use Thread.sleep() - it is the root to all evil. Instead, use a Handler
and its postDelayed( Runnable, time )
-method like below:
public class Activity1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.B);
final ProgressBar p=(ProgressBar) findViewById(R.id.pr);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
p.setVisibility(4);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent myIntent = new Intent(view.getContext(), activity2.class);
startActivityForResult(myIntent, 0);
}
}, 5000);
}
});
}
Upvotes: 4
Reputation: 29632
First of all in the Above code, you need to start the Thread using this.
t.start();
you can also try below code,
new Thread ( new Runnable() { public void run() { // Place your Intent Code here } }.start();
Upvotes: 3
Reputation: 5157
You better use an AsyncTask for this purpose.Using threads like that in your activity is not proper and can lead to some fails.Check this docs about AsyncTask.
http://developer.android.com/resources/articles/painless-threading.html
Upvotes: 4