1

so i have this problem where i have something to pass back to the activity, but the problem is i don't know how, i've read some threads but its a bit cloudy and confusing, can anyone of your shed some light about this. i just want to pass something back to the activity from my reciever (BroadcastReciever). I have read a thread that you can start an activity but the problem is that there is a bit of a confusion if passing an extra on your intent to an activity is allowed?. Thanks for any inputs any of you could provide.

lemoncodes
  • 2,371
  • 11
  • 40
  • 66
  • possible duplicate of [How do I pass data from a BroadcastReceiver through to an Activity being started?](http://stackoverflow.com/questions/2616859/how-do-i-pass-data-from-a-broadcastreceiver-through-to-an-activity-being-started) – Pratik Sep 24 '12 at 08:42
  • 1
    I guess the answer in http://stackoverflow.com/a/8597528/1434631 may help you.. – Nermeen Sep 24 '12 at 08:43
  • yep i've read both those thread, im still confused if onResume() is really called or what, and if yes, i'll put something to the onResume() isntead of in the onCreate() – lemoncodes Sep 24 '12 at 08:50
  • anyone knows how to pass something without starting an activity, cuz in my app, activity is already started, i just want to send something to that started activity, cuz starting an activity that is already started is kinda redundant – lemoncodes Sep 24 '12 at 11:12

2 Answers2

2

Possible duplicate answer https://stackoverflow.com/a/6857648/760489

you can do this way by setting flag in intent when you start the activity from broadcast receiver

public void onReceive(Context context, Intent intent){
    Intent i = new Intent(context, DestinationActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    i.putExtra("PATH", path);
    context.startActivity(i);
}

The FLAG_ACTIVITY_SINGLE_TOP makes sure the apps doesn't re-open if already open. This means that the "old" intent that opened YourActivity in the first place is re-used and it will NOT contain the extra values. You have to catch them in another method called onNewIntent() in YourActivity.

Check out the complete answer from top link

Community
  • 1
  • 1
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • shouldn't be setFlags() instead of addFlags? cuz i got mine working using ur algo but im using setFlags(), addFlags() does nothing on my case.. – lemoncodes Sep 24 '12 at 15:26
  • decided to accept this answer its the closet thing i've got, just changed the addFlags to setFlags, but still my question stays the same as above – lemoncodes Sep 24 '12 at 15:44
1

Try this out:

public void onReceive(Context context, Intent intent)
{

    Intent i = new Intent(context, DestinationActivity.class);
    i.putExtra("PATH", path);
    context.startActivity(i);
}
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
  • not really, already know this process what i'm after is passing something without starting an activty, cuz in my app the activty is already started – lemoncodes Sep 24 '12 at 11:10
  • @lemoncodes-Then it will come in front...no harm in that...but if u need to pass it without affecting ur GUI, then use Services !! – Name is Nilay Sep 24 '12 at 11:13