ProgressDialog shows up after thread is done

2019-08-14 11:15发布

I have the following three classes:

When I try to show a progressDialog when the WorkingThread is running, the ProgressDialog only shows up after the WorkingThread is done. What am I doing wrong?

I am not interested in using an AsyncTask!

-StartActivity:

public class StartActivity extends Activity implements OnClickListener
{
    public ProgressDialog pgd;

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

        imgv = (ImageView)findViewById(R.id.imageView1);
        tv = (TextView)findViewById(R.id.textview);

        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

    public void onClick(View v) 
    {
        pgd = ProgressDialog.show(StartActivity.this, "", "Loading picture"); // Start ProgressDialog before starting activity

        Intent ActivityIntent = new Intent(this, FirstActivity.class);
        startActivityForResult(ActivityIntent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0 && resultCode == RESULT_OK)
        {
            pgd.dismiss(); //Stop ProgressDialog when FirstActivity is "done"
        }
    }
}

-

-FirstActivity:

public class FirstActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        WorkingThread wt = new WorkingThread();
        wt.start();

        try 
        {
            wt.join();
            Intent ActivityIntent = getIntent();
            setResult(RESULT_OK, ActivityIntent);
            finish();
        } 
        catch (Exception e) 
        {
        }
    }
}

-WorkingThread:

public class WorkingThread extends Thread 
{

    @Override
    public void run() 
    {
        super.run();

        try
        {
            Thread.sleep(5000);
        }
        catch (Exception e)
        {
        }
    }
}

3条回答
女痞
2楼-- · 2019-08-14 11:50

Use an AsyncTask. Here's an example:

package com.example.test;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

ProgressDialog activityProgressDialog;
private Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this;

    Button btn = (Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            new TestAsyncTask().execute();
        }
    });
}

private class TestAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        //Called before doInBackground.
        //Initialize your progressDialog here.
        activityProgressDialog = ProgressDialog.show(context,
                "Test",
                "Doing heavy work on background...", false,
                false);
    }

    @Override
    protected Void doInBackground(Void... v) {

        //Do your work here
        //Important!!! Update any UI element on preExcecute and
                    //onPostExcecute not in this method or else you will get an 
                    //exception.
        //The below code just make the thread inactive for 5 seconds.
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Thread.currentThread().notify();
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        //Dismiss the progessDialog here.
        activityProgressDialog.dismiss();
    }
}

 }

I just show your edit that you don't want to use an AsyncTask. I will not delete this answer (unless you want me to) since it is another way of doing what you wanted to do.

查看更多
女痞
3楼-- · 2019-08-14 12:01

The problem is ProgressDialog always need current Activity context for display.But in your case ProgressDialog is little unfortunate

The reason is as soon as you fire ProgressDialog the next couple of lines take out Context from Current activity and starts Next Activity i.e FirstActivity.So your progressDialog gets no chance to present itself on the Screen.

查看更多
迷人小祖宗
4楼-- · 2019-08-14 12:07
try 
        {
            wt.join();
            Intent ActivityIntent = getIntent();
            setResult(RESULT_OK, ActivityIntent);
            finish();
        }

here is your problem. The UI thread is waiting the Working Thread to finish becaouse of the join().

查看更多
登录 后发表回答