ProgressDialog doesn't show when I use Intent.
-
It's working normally, and the second Asmilitis starts, but...
ProgressDialog
It doesn't show.I can't understand why it doesn't work. I saw a lot of similar code and my code is almost like it.
Can anyone find a mistake and tell us how to fix it?public class StartActivity extends AppCompatActivity {
private Intent mIntent; private final int totalProgressTime = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); } public void onClick(View view) { new DownloadTask().execute(); mIntent = new Intent(this, MainActivity.class); startActivity(mIntent); } private class DownloadTask extends AsyncTask<String,Void,Object>{ ProgressDialog mIndicator = new ProgressDialog(StartActivity.this); @Override protected void onPreExecute() { super.onPreExecute(); mIndicator.setMessage("Wait.."); mIndicator.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mIndicator.setProgress(0); mIndicator.setMax(totalProgressTime ); mIndicator.show(); new Thread(new Runnable() { @Override public void run(){ int counter = 0; while(counter < totalProgressTime ){ try { Thread.sleep(300); counter ++; mIndicator.setProgress(counter); } catch (InterruptedException e) { e.printStackTrace(); } } mIndicator.dismiss(); } }).start(); } @Override protected Object doInBackground(String... params) { return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); mIndicator.dismiss(); } }
}
-
Then you need to be like this:
public class StartActivity extends Activity {
private final int totalProgressTime = 100;
private int counter = 0;
private ProgressDialog mIndicator = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIndicator = new ProgressDialog(StartActivity.this);
mIndicator.setMessage("Wait..");
mIndicator.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mIndicator.setProgress(0);
mIndicator.setMax(totalProgressTime);
new DownloadTask().execute();
}private class DownloadTask extends AsyncTask<Void, Integer, Integer> {
@Override protected void onPreExecute() { super.onPreExecute(); mIndicator.show(); } @Override protected Integer doInBackground(Void... params) { while(counter < totalProgressTime){ try { publishProgress(++counter); Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); mIndicator.setProgress(values[0]); } @Override protected void onPostExecute(Integer integer) { super.onPostExecute(integer); mIndicator.dismiss(); Intent intent = new Intent(StartActivity.this, MainActivity.class); startActivity(intent); }
}
Call startActivity onPostExecute, and roll the cycle to doInBackGround, then get rid of new Thread()
And you must indicate your activity in AndroidManifest as starting the application.
<activity android:name=".StartActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>