1

I need to send an object from one activity to another, but after startActivity(i).

Example:

Activity A calls Activity B

Something in background should update an object in Activity A, make a copy of that object (as opposed to sending the reference), and send it to Activity B (which is already started).

<activity
        android:name="gui.GUIConversacion"

        android:label="@string/app_name" >
        <intent-filter>
        <action android:name="action.action.myactionstring" />
    </intent-filter>
    </activity>

                Intent intent = new Intent("action.action.myactionstring");
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            intent.putExtra("conversacionActualizada",conversacionesCache.get(c));
            startActivity(intent);

 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if ("action.action.myactionstring".equals(intent.getAction())) {
        this.conversacion=(Conversacion) intent.getExtras().get("conversacionActualizada");
        actualizarGUI();
    }
}
Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
user3235831
  • 45
  • 11
  • check this http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – Piyush Agarwal Jan 25 '14 at 19:33
  • I know this. It is for before to start. I need after start it – user3235831 Jan 25 '14 at 19:35
  • 1
    **"Someone in background update a object in Activity A"** - If you've already started Activity B there should be no way that anything in Activity A could be updated. – Squonk Jan 25 '14 at 20:01
  • `"I know this"` -> please, write down what you know in your Question, so people don't waste time suggesting things *you already know*. – brasofilo Jan 25 '14 at 20:08
  • @b_ he DID start the question explicitly saying that it was after Activity B is started (ie not starting a new activity) so pyus was wrong here – Nick Cardoso Jan 25 '14 at 20:14

1 Answers1

0

What you need to do is send another intent with the extra attached and a flag to say to reuse he activity, it will then call onNewIntent on the receiving activity where you can get that object.

Edit: It's bad practice to link without summarising, so here's what you need:

In Receiving activity:

   public static final String EXTRA_KEY_CONSTANT = "my extra key";

   @Override
   protected void onNewIntent(Intent intent) {
       super.onNewIntent(intent);
       if ("action.action.myactionstring".equals(intent.getAction())) {
           //TODO: intent.getExtra(EXTRA_KEY_CONSTANT);
       }
   }

In receiving activity mainfest

   <activity android:name=".MyReceivingActivity" >
        <intent-filter>
            <action android:name="action.action.myactionstring" />
            <category android:name="android.intent.category.DEFAULT" />
            ...
        </intent-filter>
   </activity>

In the other activity

    Intent intent = new Intent("action.action.myactionstring");
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(ReceivingActiviy.EXTRA_KEY_CONSTANT, object);
    startActivity(intent);
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • How is this going to be used in Activity A if Activity B is already started? – Squonk Jan 25 '14 at 20:05
  • This says how to get A to contact already started B - as the question asked. It's not my concern how Activity A receives the background update and starts this process. – Nick Cardoso Jan 25 '14 at 20:12
  • You miss my point - if Activity A has already started Activity B, i.e., Activity B is visible and running, Activity A will be hidden and, at the very least, in a non-running state - either paused or more usually stopped. It should not be possible for anything in Activity A to be updated nor execute any code in this state. – Squonk Jan 25 '14 at 20:25
  • I know that @Squonk, but the question asked for "sent an object from an activity to other after startActivity" and the solution that I have provided does that. It's not my place to be concerned with other lifecycle changes outside of that question and his architecture and in any case, you've already made him aware of this – Nick Cardoso Jan 25 '14 at 20:28
  • when I do startActivity(intent) in the other activity, there is an excepction: – user3235831 Jan 26 '14 at 15:12
  • 01-26 16:12:28.397: E/AndroidRuntime(28181): FATAL EXCEPTION: main 01-26 16:12:28.397: E/AndroidRuntime(28181): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=action.action.myactionstring flg=0x20000000 (has extras) } 01-26 16:12:28.397: E/AndroidRuntime(28181): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 01-26 16:12:28.397: E/AndroidRuntime(28181): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423) – user3235831 Jan 26 '14 at 15:13
  • Looks like you haven't set up your intent filter. Edit your question above, attach your manifest and the two other parts you changed to match my example – Nick Cardoso Jan 26 '14 at 15:17
  • I've edited my answer, you'll see I've added a category to the intent filter, in simple terms it means "when this action is looking for an activity, throw my name in the hat", you should also change the action string to something specific to you, containing your package name and the name of the action – Nick Cardoso Jan 26 '14 at 15:32
  • it's running!! thanks you so much!! I have I nullpointer but I think that is for other things! very thanks you! – user3235831 Jan 26 '14 at 15:35
  • If my answer has pointed you in the right direction please mark it as the accepted answer now, thank you – Nick Cardoso Jan 27 '14 at 03:12