Clearning Activity's intent data randomly comes back, why?
Asked Answered
R

4

8

I have a media application which starts playback when the intent is sent to the player activity with the following intent extras; data "path to the music" and type "mime/audio format".

I pick up the intent data at the player activity execute to start playback and I remove the passed extras from the intent to avoid having the same request going again after flipping the screen or the activity being brought back to the foreground.

This is how I process an intent:

final String data = getIntent().getDataString();
final String type = getIntent().getType();

// start playback
    requestPlay( data, type );

    // remove intents because they are needed only once per call!
    this.getIntent().setDataAndType(Uri.parse(""), "");
    this.getIntent().removeExtra("data");
    this.getIntent().removeExtra("type");

The issue I'm having is that randomly and rarely, I will open the application and when it resumes at the player activity, the intent will contain the previous extra data and start playing... This is annoying to me and well my users...

Anyone have any ideas what's the best way to clear the intents data? Some reason the ActivityManager might be keeping this data stored...?

Thanks!

-Jona

Rebeccarebecka answered 17/2, 2011 at 17:5 Comment(0)
Z
4

My Solution:

Bundle mExtrass = null;
getIntent().replaceExtras(mExtrass);

And this clear the extra data.

Zareba answered 5/9, 2011 at 11:31 Comment(0)
S
0

Unclear on this, but have you tried Intent flags FLAG_ACTIVITY_RESET_TASK_IF_NEEDED, FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, or FLAG_ACTIVITY_NEW_TASK?

Selenite answered 17/2, 2011 at 18:18 Comment(0)
S
0

When retrieving the data and type strings, try passing them as extras instead of using getDataString() and getType():

final String data = getIntent().getExtras().getString("music_path"); 
final String type = getIntent().getExtras().getString("data_type");

// start playback
    requestPlay( data, type );

    // remove intents because they are needed only once per call!
    this.getIntent().setDataAndType(data, type);
    this.getIntent().removeExtra("music_path");
    this.getIntent().removeExtra("data_type");

Before, you'll need to pass the types as extras:

intent.putExtra("music_path", <path to music>);
intent.putExtra("data_type", <mime/audio format);
Sibley answered 17/2, 2011 at 19:13 Comment(1)
The problem is that my application is media application that registers for supported mime types playable by the app. So if a user clicks on an mp3 file my app shows as a choice for playing... Once clicked to play via my app I need to call getDataString() and getType() to get the mp3 info for playback.Rebeccarebecka
S
0

I've experienced the same issue. In my case the problem was that I sometimes got two intents on application startup: one in onCreate and then another in onNewIntent. The intent from onCreate had some really old data attached (that should have been cleared according to the docs), while the onNewIntent had no extra data (the correct case).

My solution was to record the last incoming intent (assuming that the last intent is the correct one) and then handle the intent in onResume since that method is called after onCreate and onNewIntent. Like this:

private Intent lastIntent;

public void onCreate()
{
    lastIntent = getIntent();
}

public void onNewIntent(Intent intent)
{
    lastIntent = intent;
}

public void onResume()
{
    if (lastIntent != null)
    {
        // handle lastIntent and then set it to null
        lastIntent = null;
    }
}
Succory answered 20/5, 2011 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.