Note that there are some explanatory texts on larger screens.

plurals
  1. POStarting an android activity from a worker thread?Is this a good practice
    text
    copied!<p>What i am trying to do over here is that I want to call a webservice and based on its response i might invoke another webservice or start an activity.I am already writing the web service in a separate thread but the issue is that i am calling the activity in a worker thread, To make myself more clear i have put my <strong>pseudo code</strong>.</p> <pre><code>if (User ID and Password present in the shared preference) THEN Utils.checkauthorisation(API) //Web Service Call if(respsonse is Paswordexpired) erase password from DB Goto (LOGIN SCREEN)//startActivity Call else if(download of images hasnt happened today)) THEN UTILS.DownloadImages//Web service call if(response==connectivityorOtherError) Toast respective Message GOTO (GALLERY SCREEN)//startActivity Call else if (response==confilicted Data) Goto (CHANGES SCREEN)//startActivity Call endif endif endif </code></pre> <p>I was planning to show a progress bar and do all these events in a thread like this</p> <pre><code> progressDialog = ProgressDialog.show(this, "Loading", "Authenticating Please wait."); new Thread() { public void run() { ///execute the pseudo code startActivity(intent); //Is this a good practice Message msg = Message.obtain(); msg.what = 1; messagHandler.sendMessage(msg); } }.start(); private static Handler messagHandler = new Handler() { public void handleMessage(Message message) { super.handleMessage(message); switch (message.what) { case 1: progressDialog.dismiss(); break; default: break; } } }; </code></pre> <p>But something that disturbs me is that I have to <strong>start an activity in a worker thread</strong> here. <strong>Is this good practice?</strong> I initially thought that we could only start an activity from the UI thread. What is the process happening in the back end here(in the thread sense)? If this is not a good practice what are the other alternatives to implement my pseudocode?</p> <p>P.S.:Sorry about asking this question again.I had asked the same question earlier but had failed to convey what i wanted exactly so had received answers for alternatives to starting activity on worker thread(part of my question) but didn't recieve any answer on "is this a good practice" </p> <p>Thanks</p>
 

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