Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the new Intent in onNewIntent()?

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.

like image 784
user588132 Avatar asked Jan 24 '11 21:01

user588132


People also ask

What is on new intent in android?

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 .

How do you initialize an intent?

You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data. If you want to receive a result from the activity when it finishes, call startActivityForResult().

How do you use onNewIntent?

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). If you set to single top, the activity will not be launched if it is already running at the top of the history stack.

How do I start an intent in Java?

This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);


3 Answers

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.

like image 138
Christopher Orr Avatar answered Oct 05 '22 23:10

Christopher Orr


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.

like image 25
Torid Avatar answered Oct 05 '22 23:10

Torid


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
    }
}
like image 26
Vikalp Patel Avatar answered Oct 05 '22 23:10

Vikalp Patel