Adeesha
Adeesha

Reputation: 55

How to receive multiple intents to a single activity from different activities?

I am new to android development. In my application I have a broadcast receiver R and 4 other activities, say A,B,C,D. When R received a SMS, R sends some data to A, B and C activities using intents. Sending data for those 3 activities works fine. Using sent data for A,B and C activities they implement their own tasks and produce some results. Now each of those A,B and C activities should send the produced results to Activity D. I used intents to implement this and data is sent as bundles. What I expect is activity D collects all the sent data(results) from A, B and C and display those in a single Text view. But what is being displayed on the Text view is, only the result made by Activity C and two null values. So, if you know how to overcome this issue please help me. It will be highly appreciated.

Upvotes: 1

Views: 3434

Answers (3)

Adeesha
Adeesha

Reputation: 55

Bundle b = new Bundle();
splitMsg = smsMsg.split("\\n");

for(int i=0; i<splitMsg.length; i++)
{
  if(!(splitMsg[i].equalsIgnoreCase("null")))
  {
     splitFeatures = splitMsg[i].split(":");               

     if(splitFeatures[0].equals("SomethingToA"))
     {

       String valueA = splitFeatures[1];
       b.clear();
       b.putString("keyA", valueA);

       Intent ia = new Intent(context, A.class);
       iaContactName.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       iaContactName.putExtras(b);
       context.startActivity(ia);  
     } 

     if(splitFeatures[0].equals("somethingToB"))
     {
       String valueB = splitFeatures[1];
       b.clear();
       b.putString("keyB", valueB);   
       Intent ib = new Intent(context, B.class);
       ib.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

       ib.putExtras(b);
       context.startActivity(ib);
     } 

     if(splitFeatures[0].equals("SomethingToC"))
     {
       String valueC = splitFeatures[1];
       b.clear();
       b.putString("keyC", unreadSms);

       Intent ic = new Intent(context, C.class);
       ic.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       ic.putExtras(b);
       context.startActivity(ic);
     } 
   }
 }

Now above code snippet is the part I have used to send data from R receiver to activities A,B and C. Each of those 3 activities receives appropriate intent which has been sent to them and will extract the bundled data and use those data to produce some results.

Then the produced results will be bundled and sent to the activity D. This is how it's done.

Bundle b = new Bundle();
 b.putString("keyD", valueD);
 Intent id = new Intent(A.this,D.class);
 id.putExtras(b);
 startActivity(id); 
 Each of A,B and C activities send results to D as in the above. Then D will try to  
 extract data from intents as like this, 

   Bundle extras = getIntent().getExtras();



if(extras != null)
   {
      String fromA = extras.etString("keyA");           
      String fromB = extras.etString("keyB");           
      String fromC = extras.etString("keyC");       
      // some code goes here to append fromA, fromB, fromC together and set it into a 
         Text view. 
     }

But in the text view, it displays something like this, "null null result produced by activity C"

instead of, "result produced by activity A

result produced by activity B

result produced by activity C"

Upvotes: 0

Barkausen
Barkausen

Reputation: 126

As pointed out in the comments, maybe you are using a wrong approach.

Anyway, if you are sure about what you are doing (and if i understood correctly your problem), you could declare your activity as singleInstance or singleTask
See android:launchMode here for more details: https://developer.android.com/guide/topics/manifest/activity-element.html

I'd like to hilight that the documentation itself says

The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.


EDIT:

After reading your code: There are 2 possible ways to solve this problem

If you do NOT need user interaction, you should use an AsyncTask (https://developer.android.com/reference/android/os/AsyncTask.html): you do not need activities just to perform calculations and you should not use them in this way. If you want to be sure that every task has been completed before proceding, you might want to use an Executor. Add the tasks (new AsyncTaskClassX().executeOnExecutor(executor, params);) and then, if you want to wait for results:

executor.shutdown();
try {
    executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
    //something
}

This will cause the system to wait for the completion of ALL tasks before proceding.

If you NEED user interaction then you may consider launching activities using

startActivityForResult(intent, 0);

Using this method you launch the first activity (that takes control of the screen), when the activity has completed its operations using something like

Intent intent=new Intent();
setResult(RESULT_OK, intent);
finish();

You can gather the result overriding onActivityResult and checking the request code you can establish wich activity just finished. At this point launch B then C and use their results.

Just a note: you should have edited your question to add the code instead of add a comment. This avoids confusion when reading "complex" questions and keep everything in one place (this is also why i edited my answer and i did not added another answer).

Upvotes: 2

Robert Estivill
Robert Estivill

Reputation: 12477

Activities are usually associated with UI. You can not have 3 activities running at the same time, since they are paused when another one gets on top.

If the process that the three (A,B,C) activities are doing doesn't require UI, i would move it to a Service or Thread, and have it report back to the D activity using a broadcast intent.

If it does require UI, you will have to do it sequentially to ensure that the activity is not interrupted with another of your own activities. So, launch activity A, fill a new intent with it's result, and start activity B. Then B would calculate its own thing, put it together with A's result in a new intent a start C. Same thing with C activity, until it delivers all results to activity D.

I would definitely recommend you to read through the activity lifecycle documentation.

Good luck

Upvotes: 1

Related Questions