How to capture the new Intent in onNewIntent()?
Asked Answered
G

5

44

I want to pass a new, but different, Intent to an Activity. How do you differentiate between the new Intent and previous Intents? What kind of code goes into onNewIntent()? An example will be very helpful.

Gunboat answered 24/1, 2011 at 21:15 Comment(0)
I
93

The new intent comes as part of onNewIntent(Intent). The original Intent is still available via getIntent().

You put whatever code you need to into onNewIntent in order to update the UI with the new parameters; probably similar to what you're doing in onCreate.

Also, you probably want to call setIntent(intent) in onNewIntent to make sure future calls to getIntent() within the Activity lifecycle get the most recent Intent data.

Interweave answered 24/1, 2011 at 21:57 Comment(3)
Thank you, So when the activity is called the control goes directly to onNewIntent()?Gunboat
If you fire an Intent that causes an existing Activity to be re-used, yes.Interweave
@ChristopherOrr my onNewIntent() never get launched, would you please share with me some idea?Breakthrough
S
24

How an intent arrives at your activity depends on the launchMode (see the launchmode docs at http://developer.android.com/guide/topics/manifest/activity-element.html#lmode).

  • For launchmode "standard" (the default) a startActivity with a new intent will result in an onCreate with that intent to a new instance of the activity.

  • For launchmodes "singleTop" and "singleTask" a startActivity with a new intent will result in either

a) an onCreate with that intent to a new instance of the activity (if that activity was not running) [as per "standard" above] or b) an onNewIntent with that intent to the existing activity (if that activity was already runnning).

For b), the second intent is available in the onNewIntent parameters. What you do with it depends on your application. Some applications will just ignore it, while others will do a setIntent() and start re-initialization / update processing the new intent.

Shikari answered 27/4, 2011 at 20:43 Comment(1)
currently i'm using singleTask, but both method not fired in my code according logs... :(Showalter
L
13

Your Called Activity-Main Activity

public class MainActivity extends Activity
{
    public void onCreate(Bundle SavedInstanceState)
    {
    }

    @Override
    protected void onNewIntent(Intent intent) 
    {
        super.onNewIntent(intent);
        if(intent.getStringExtra("methodName").equals("myMethod"))
        {
            myMethod();
        }
    }

    public void myMethod()
    {
    }
}

Your Calling Activity

Code goes to Previous Intent

public class CallingActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        Intent i=new Intent(this,MainActivity.class);
        i.putExtra("methodName","myMethod");//goes to previous INtent
        startActivity(i);//will trigger only myMethod in MainActivity
    }
}

Your Calling Activity

Code Starts New Activity using these kind of intent

public class CallingActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        Intent i=new Intent(this,MainActivity.class);
        startActivity(i);//will trigger New Activity i.e. MainActivity
    }
}
Leonteen answered 17/8, 2012 at 9:23 Comment(2)
protected void onNewIntent(Intent intent) in MainActivity not fired according you explanation. Can u please give some example according my code? Help will appreciated...Showalter
@Vikalp Patel your code is very interesting and useful, but you have used 3 different versions of the "mymethod" string in the code. would it be possible for you to correct this? or are the different case versions of the string intentional. you have "Mymethod", "mymethod" and "myMethod"Exarchate
S
3

There's a bug related to this : http://code.google.com/p/android/issues/detail?id=17137

Sulphurate answered 22/12, 2011 at 18:28 Comment(0)
F
0

If you don't want your activity to re-use the new intent in every subsequent onResume() I would recommend to store the intent in an instance field instead of via setIntent(intent).

That way you can reset that instance field to null once you have consumed the intent without throwing away the original launch intent.

More details in my answer here: https://mcmap.net/q/374931/-is-there-any-reason-not-to-call-setintent-when-overriding-onnewintent

setIntent(Intent) has been described as a mistake by an Android Framework Engineer: https://groups.google.com/forum/#!topic/android-developers/vrLdM5mKeoY

Florencio answered 21/1, 2014 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.