0

I have a class in my Android app, called Main.java, to validate a user login (user name + password) against the data in my server. At first, I succeeded; I used an AsyncTask thread to do it plus a library which handles the Http connection, call HttpPostAux.java (in fact, I found the library's code here in this forum). In the onPostExecute method of AsyncTask, I was creating and starting a new activity instead of modifying the current one and it worked.

But now I want to do things different. I want to save the validated data (user name + password) into a SQLite table in the AsyncTask thread and then in the UI thread, recover that data and use it to open the mentioned activity. The insertion occurs but when I'm trying to access the database from UI thread: it says that the table is empty. So I looked in the logcat and I found that UI thread executes before AsyncTask thread.

So my question is how to insert data in the AsyncTask thread and then recover it inside UI thread? Can anybody help here? I'm kind of lost!

I will appreciate a code example! Thanks in advance! Greetings from Venezuela!

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
alois.wirkes
  • 369
  • 2
  • 7
  • 20

4 Answers4

5

UI thread is your applications main thread. When you create an AsyncTask, your long time-taking task will be executed(inside doInBackground function) on a separate thread. When doInBackground completes, onPostExecute() will be called from the UI thread. So you simply need to execute your UI thread task(="recover that data and use it to open the mentioned activity") from inside onPostExecute().

Caner
  • 57,267
  • 35
  • 174
  • 180
0

I think the way you did it before is correct. I would recommend to make a sort of SplashScreen activity which checks if the user has been logged in before, i.e. if there is a username/password in the database. If this is the case, use this data to login the user and then proceed to your main activity. If the user hasn't been logged in before, promp them with a login screen, store this data for future use and continue to your main activity.

Joris
  • 1,437
  • 1
  • 15
  • 22
0

You can implement a BroadcastReciever in your UIThread that listens to Intents sent from your AsyncTask when the insert to the database is done.

fire from AsyncTask:

Intent intent = new Intent("DATABASE_INSERTION_DONE");
context.sendBroadcast(intent );

register in UiThread:

IntentFilter intentFilter = new IntentFilter("DATABASE_INSERTION_DONE");
registerReceiver(myIntentsReceiver, intentFilter);

class:

class MyIntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if ("DATABASE_INSERTION_DONE".equals(intent.getAction())) {
            //do something
        }
    }
}
jelgh
  • 705
  • 1
  • 6
  • 22
0

You have to keep in mind that once you start an AsyncTask, it will run on another thread and your UI thread will keep moving forward. For example:

x="foo";
new SampleAsyncTask().execute();
txtView.setText(x);

Class SampleAsyncTask extends AsyncTask<...>{
.....

 public Void doInBackground(...){

 //LOTS OF CALCULATIONS

 x="bar";
 }  
}

Your txtView will most definitely show "foo" rather than "bar". To make sure that your AsyncTask is finished there are lots of options. You can call a method to start the rest of your operations in PostExecute of your AsyncTask. You can also use intents and a broadcastreceiver like Jelgh said. But my favorite is using a simple callback:

public class LoginActivity extends Activity implement SampleAsyncTask.asyncCallback{

@Override
protected void onCreate(Bundle savedInstanceState){
x="foo";
new SampleAsyncTask().execute();

Class SampleAsyncTask extends AsyncTask<...>{ 

   asyncCallback mCallback;

   public SampleAsyncTask(Context c){
   mCallback; = (asyncCallback) c
   }

   public interface asyncCallback{
    void servedMyPurposeYouCanGoOn();
   }

   public Void doInBackground(...){

   //LOTS OF CALCULATIONS

   x="bar";
   mCallback.servedMyPurposeYouCanGoOn;

   //REST OF THE WORK

   }  
 }
}

@Override
public void servedMyPurposeYouCanGoOn(){
runOnUIThread(
//rest of the work
)   
}
}
SoroushA
  • 2,043
  • 1
  • 13
  • 29