1

In searchactivity: I declared a background worker class to connect to the remote mysql database and get some data

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    BackgroundWorker worker = new BackgroundWorker(SearchActivity.this);
    worker.execute("Searchfirst");
    ////////// My message is empty below
Toast.makeText(SearchActivity.this,getIntent().getExtras().getString("searchresult"),Toast.LENGTH_SHORT).show();
}

In backgroundworker.class, the backgroundworker class did get the data but fails to pass to my activity

protected void onPostExecute(Wrapper w) {
    else if(w.result.contains("Searchfirst successfully!")){
        ////////// My message can be showed correctly here
Toast.makeText(context,w.result.toString(),Toast.LENGTH_SHORT).show(); 
        Intent colleges = new Intent(context,SearchActivity.class);
        colleges.putExtra("searchresult", w.result);
    }
}

Also I found out that if i put the data to the intent and start that activity immedicately by calling startActivity(intent) in onPostExecute(), the data can be passed to that intent, however, if i did not start that activity, the data is lost?

KKKK
  • 347
  • 7
  • 18
  • Creating an `Intent` and putting an extra in it does not magically send that data to your activity. Usually you send the data back to the activity or whatever is interested in the results using an interface with a callback as shown [here](http://stackoverflow.com/questions/9447646/how-do-i-send-data-back-from-onpostexecute-in-an-asynctask). – George Mulligan Feb 08 '16 at 20:03
  • Actually I saw your link before, but everytime i tested it there comes another problems. – KKKK Feb 08 '16 at 20:04
  • What kind of other problems? – George Mulligan Feb 08 '16 at 20:05
  • It is hard to say, like i try implementing for log in and log in works, but my register activity then crashed. – KKKK Feb 08 '16 at 20:05
  • You can look at the logcat output to see the reason why it crashed. – George Mulligan Feb 08 '16 at 20:08

1 Answers1

2

You can add this lines:

Intent intent = new Intent("myAction");
intent.putExtra("searchResult", w.result);
sendBroadcast(intent);

then register a broadcast reciever in your activity:

registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Object result = intent.getExtras().get("searchResult");
            }
        }, new IntentFilter("myAction"));

Besides, You can define a listener interface and pass it to the async task, then call the listener's method in onPostExecute() method. There is no need to calling an intent this way.

Shayan_Aryan
  • 2,002
  • 1
  • 29
  • 31
  • Here is the problem that I do not want to start a new activity. – KKKK Feb 08 '16 at 20:12
  • How does this help? OP mentioned `startActivity` was already used and tried using a callback interface but couldn't get it to work... – George Mulligan Feb 08 '16 at 20:13
  • @KKK calling startActivity(intent) not always start a new activity. You can broadcast an intent to all receivers with that intent filter. I updated my answer – Shayan_Aryan Feb 08 '16 at 20:17
  • for the interface listener method, where should i write the code for verifying the data from asynctask and do other stuffs? In the method that implements the interface in the activity? where should i call this method? – KKKK Feb 08 '16 at 20:28
  • @Shayan_Aryan Where did you get the idea that calling `startActivity` with an action only will trigger a broadcast receiver listening for that action? – George Mulligan Feb 08 '16 at 20:32
  • @GeorgeMulligan sorry for that, I meant sendBroadcast(intent) method. – Shayan_Aryan Feb 08 '16 at 20:36
  • @Shayan_Aryan can you post the code in the activity to declare the receiver and toast.result of data? – KKKK Feb 08 '16 at 20:39
  • @KKK for the receiver way, implement a onWrap(Wrapper w) method in your activity (your activity must implement your custom Listener), then call the listener.onWrap(w) method in onPostExecute() – Shayan_Aryan Feb 08 '16 at 20:39