2

Android: new Intent() starts new instance with android:launchMode="singleTop"

i got single top to work as per the link above, but I am having a hard time putting "extras" in the intent and then performing a function on my original activity.. is this possible?

Intent I= new Intent(context, away.class);
I.putExtra("number",  number);
I.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |       Intent.FLAG_ACTIVITY_SINGLE_TOP);

this snippet is from my broadcast receiver and it refers back to my main class.. in my main class my code is like so..

Intent I = getIntent();
int number = I.getIntExtra("number", -1);

so my question is the following.. how can i get my main activity to evaluate the number i send back and then fire a function when my receiver class fires it?

Community
  • 1
  • 1
Dnaso
  • 1,335
  • 4
  • 22
  • 48

1 Answers1

2

You have to override onNewIntent and get the extra there.

You have to override onNewIntent and get the extra there.  

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);


    int number = intent.getIntExtra("number", -1);
}  

In your broadcast receiver

Intent I = new Intent(context, away.class);
I.putExtra("number",  number);
Log.d("here", "number = " + number);
I.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(I);
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • ok so now my app doesn't crash.. from the onNewIntent can I fire a function? i tried and it doesnt do anything.. – Dnaso Mar 27 '13 at 21:25
  • onNewIntent is called before onResume. I do not know what function you try to call. Called it onResume. – Hoan Nguyen Mar 27 '13 at 21:28
  • I didnt get that far, I just tried something arbitrary like a toast to say hey.. but the thing is i thought onResume is if you leave your activity?(Btw im a web programmer and I am trying to learn java so please bear with me..) – Dnaso Mar 27 '13 at 21:35
  • i just added what i added to my main class – Dnaso Mar 27 '13 at 21:37
  • onResume mean you come back to the activity, onPause mean you are leaving. Instead of toast just put Log.d("onNewIntent", "number = " + number) and see if logcat show the right number. – Hoan Nguyen Mar 27 '13 at 21:37
  • But the thing is I am never leaving my activity because my other class is a broadcast receiver not an intent that I am coming back from... – Dnaso Mar 27 '13 at 21:38
  • Ok still you get it in onNewIntent, just log it and see what happen. – Hoan Nguyen Mar 27 '13 at 21:39
  • did i have to change anything in my other class? – Dnaso Mar 27 '13 at 21:43
  • Just set it as I.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) – Hoan Nguyen Mar 27 '13 at 21:45
  • nope. i and I tried to take it out all together still same thing – Dnaso Mar 27 '13 at 21:47
  • the number is fine though i tried to log it when i fire my intent so I am not sure whats going on.. the oN new intent is not firiing – Dnaso Mar 27 '13 at 21:49
  • I did not see startActivity(I) in your broadcast receiver – Hoan Nguyen Mar 27 '13 at 21:51
  • yes because eclipse gave me an error saying it wasnt a method of the broadcast receiver – Dnaso Mar 28 '13 at 00:09
  • my app actually just crashes now because it wont let me call the context from inside the broadcast receiver idk y – Dnaso Mar 28 '13 at 00:30
  • public void onReceive(Context context, Intent intent) use the context parameter – Hoan Nguyen Mar 28 '13 at 00:32
  • I did.. eclipse logcat said that context requrire flag_activity new task flag.. i – Dnaso Mar 28 '13 at 00:34
  • oh jeez, how obvious they were out of order and fire sequentially. I feel like an idiot. – Dnaso Mar 28 '13 at 00:40