Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I show a Progress dialog between two activities?
    text
    copied!<p>I have a problem. I need to go from one activity to another. My second activity has a big xml layout, with a lot of elements (I'm speaking about four hundred aprox.), then it takes a few seconds (too much) to show this second Activity.</p> <p>How can I show a Progress dialog between two activities?</p> <p>I'm trying to use a background task to do this.</p> <p>I have this method in my Activity A:</p> <pre><code> private void goToYear() { Intent intent = new Intent(); intent.setClass (getBaseContext(), YearActivity.class); startActivity( intent); } </code></pre> <p>And in my Activity B:</p> <pre><code>public class YearActivity extends Activity { private String TAG = "YearActivity ::"; private ProgressDialog pd = null; @Override public void onCreate( Bundle savedInstanceState) { super.onCreate( savedInstanceState); // Show the ProgressDialog on this thread this.pd = ProgressDialog.show(this, "Working...", "Calculating the screen...", true, false); // Start a new thread that will download all the data new MakeYearTask().execute(); } private void initCalendar () { this.setContentView( R.layout.calendar_year); ... ... initialize values ... ... } private class MakeYearTask extends AsyncTask&lt;String, Void, Object&gt; { protected Void doInBackground(String... args) { Log.i("YearActivity::MakeYearTask", "MakeYearTask Background thread starting"); YearActivity.this.initCalendar(); } protected void onPostExecute(Object result) { if (YearActivity.this.pd != null) { YearActivity.this.pd.dismiss(); } } } } </code></pre> <p>You can see that I make the <code>setContentView</code> out of the <code>onCreate</code> method.</p> <p>This doesn't work. It gives me one exception, like this:</p> <pre><code>12-09 19:49:17.729: ERROR/AndroidRuntime(218): java.lang.RuntimeException: An error occured while executing doInBackground() 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.os.AsyncTask$3.done(AsyncTask.java:200) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at java.util.concurrent.FutureTask.run(FutureTask.java:122) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at java.lang.Thread.run(Thread.java:1060) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.view.ViewRoot.checkThread(ViewRoot.java:2629) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.view.ViewRoot.requestLayout(ViewRoot.java:545) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.view.View.requestLayout(View.java:7657) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.view.ViewGroup.addView(ViewGroup.java:1749) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.view.ViewGroup.addView(ViewGroup.java:1731) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2186) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2239) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.app.Activity.setContentView(Activity.java:1620) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at es.jota.app.YearActivity.initCalendar(YearActivity.java:42) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at es.jota.app.YearActivity.access$0(YearActivity.java:41) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at es.jota.app.YearActivity.initCalendar$MakeYearTask.doInBackground(YearActivity.java:120) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at es.jota.app.YearActivity.initCalendar$MakeYearTask.doInBackground(YearActivity.java:1) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at android.os.AsyncTask$2.call(AsyncTask.java:185) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256) 12-09 19:49:17.729: ERROR/AndroidRuntime(218): ... 4 more </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload