2

I would like to implement the following simple pattern in an android app:

  1. Activity A starts activity B
  2. In activity B the user fills out a simple form F, then hits submit
  3. Activity B then starts an AsyncTask C that posts F to a url, and immediately returns to Activity A
  4. Back in activity A, the user continues with business as usual while C is still working. But once C is complete, the user should be notified, in particular if the result was a failure.

The problem I have is with steps 3 and 4. Since C is started by B, how can A get notified? Hmm, not sure if this is clear to anyone but me ...

Anyway, right now I am stuck with a solution where step 3 instead looks like this:

3". Activity B returns the content of the form to activity A. A then starts AsyncTask C to post the form and continues as usual.

But it just seems a bit unnecessary that A also needs to handle the form, it should just be B's responsibility. Any other ideas or solutions out there?

3 Answers 3

2

I would use a broadcast reciever. http://d.android.com/reference/android/content/BroadcastReceiver.html

In Activity B

Intent broadcastIntent = new Intent("com.yourapppackage.BROADCAST"); broadcastIntent.putExtra("myExtras", myExtras.extra); // Put your results in here sendBroadcast(broadcastIntent); 

Fire this from onPostExecute of your AsyncTask.

In your application manifest add an intent filter to your activity A entry http://developer.android.com/guide/topics/manifest/intent-filter-element.html

<activity android:name=".A" > <intent-filter> <action android:name="com.yourapppackage.BROADCAST" /> </intent-filter> </activity> 

Register a BroadcastReceiver in Activity A http://developer.android.com/reference/android/content/BroadcastReceiver.html

// Set up a handler and a broadcast reciever private final Handler mHandler = new Handler(); private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Handle reciever String mAction = intent.getAction(); if(mAction.equals("com.yourapppackage.BROADCAST")) { // Do something with the results - intent.getExtras(); } } } // Register a reciever IntentFilter myRecieverIntentFilter = new IntentFilter(); myRecieverIntentFilter.addAction("com.yourapppackage.BROADCAST"); this.registerReceiver(mIntentReceiver, myRecieverIntentFilter, null, mHandler); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I haven't looked into broadcast yet, just seemed a bit overkill since I just have one receiver :)
1

Try this way out. http://masl.cis.gvsu.edu/2010/04/05/android-code-sample-asynchronous-http-connections/

Comments

0

Why not have B acquire the information needed to run the task, then pass that back to A? The onPreExecute() and onPostExecute() happen in the main thread of Activity A.

Never mind, I just saw that in your description. But yes - B handles it all - A just submits the data to the URL.

1 Comment

Yes, in the end I think I can live with that. Since the form is just a set of strings pairs, A really doesn't need to know anything about it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.