Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent extras not changed

I have an existing activity with a MediaPlayer that still running after onStop() was called.

I want to reload this activity but passing it different extra values.

            Intent intent = new Intent(WelcomeScreen.this, PlayMusic.class);
            intent.putExtra("plan", 0);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);

But, that extra is "stuck" with the same values no matter what I put on the new intent.

Any ideas?

like image 223
Kobi Burnley Avatar asked Jan 20 '26 01:01

Kobi Burnley


2 Answers

As you clearly state, you create a new Intent object. Whatever were the paramters associated with the old Intent, the one that was used to initially create the Activity, they were not affected.

However, you can access the new Intent by overriding Activity's onNewIntent() method:

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

    if ( intent != null && intent.hasExtra("plan") )
    {
        // Yay! Do whatever... Live long... Prosper...
    }
}
like image 91
Vaiden Avatar answered Jan 22 '26 16:01

Vaiden


You have to set new intent explicitly in onNewIntent callback

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    setIntent(intent)
}
like image 38
Dariusz Bacinski Avatar answered Jan 22 '26 15:01

Dariusz Bacinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!