Skip to content Skip to sidebar Skip to footer

Other Thread Wont Let Me Change Activity Layout!? Android

SOLUTION: It might not be the best or the prettiest solution, but it works. Start a new thread so the main thread can display a ProgressDialog while the new thread retrieves, assig

Solution 1:

No you can't change the UI from another thread. If you have task that do not touch the UI you can use AsynchTask to run them in the background and be notified in the main thread when the task is completed. But if you need to modify the UI you need to do it in the main thread. If your code is somewhere else and it is used by other threads you can use runOnUIThread to execute the code that changes the UI

publicvoidonCreate(Bundle savedInstanceState) {
  ...
progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");
initiateActivity();
progressDialog.dismiss();

}

privatevoidinitiateActivity() {
    fillOrderTable();
    initiateGestureview();
    fillCustServiceForCustomer();
}

EDIT

Thinking about it: the difference is that you need to exit the onCreate that is what happens if you run a thread that attaches itself to the main thread. So the solution above is not equivalent to your solution for that reason.

Solution 2:

It is possible to initiate changing layout from another thread, but all the job has to be done in the UI thread. To do that you can use Activity.runOnUiThread(), View.post(), View.postDelayed() methods, or you can use a Handler.

The article Painless Threading contains more details regarding this task.

Solution 3:

You can not change UI from other threads... TO change UI you have to use "runOnUiThread" or use some handler to make changes.. try this... make your activity implement Runnable

publicclassmyclassextendsActivityimplementsRunnable{
publicvoidonCreate(Bundle savedInstanceState) {
 ......
progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");
 Threadthread=newThread(this);
            thread.start();
}
 publicvoidrun() {
            fillOrderTable();
            initiateGestureview();
            fillCustServiceForCustomer();
            handler.sendEmptyMessage(0);
    }
 privateHandlerhandler=newHandler() {
            @OverridepublicvoidhandleMessage(Message msg) {
                    progressDialog.dismiss();


            }
    };
}

Solution 4:

SOLUTION:

It might not be the best or the prettiest solution, but it works. Start a new thread so the main thread can display a ProgressDialog while the new thread retrieves, assigns and interrupts the main thread with a new runnable to initiate views. For the ProgressDialog to be shown while all the activity in the new thread is going on, the ProgressDialog must, as mentioned, be started in the main thread and dismissed of course at the end of the new thread. The ProgressDialog must be started as the last thing right before the new thread starts, and everything you wanna wait for must go into the new thread. And remember, if you wanna make UI alterations while waiting and showing ProgressDialog you must interrupt the main thread with a new runnable, which makes the alterations in it's run() method, by using runOnUiThread.

publicvoidonCreate(Bundle savedInstanceState) {
                ...

                ((ScrollView) findViewById(R.id.contentContainer)).addView(getLayoutInflater().inflate(R.layout.myCustomContentLayout, null));

                ((TextView) findViewById(R.id.name)).setText(customer.getProperty("name").getValue().toString());

                final ProgressDialog progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");

                newThread(newRunnable() {
                    protectedvoidrun() {
                        ... //get some data, fill some lists and assign some variables...runOnUiThread(newRunnable() {
                                          publicvoidrun() {
                                              initiateActivity();
                                          }
                                      });

                        progressDialog.dismiss();

                        returntrue;
                    }
                }).start();
}

Post a Comment for "Other Thread Wont Let Me Change Activity Layout!? Android"