Issue
I'm making a music app which displays a notification while audio is playing. When clicked, this notification opens the main (UI) activity through an intent.
While this should be a pretty simple process, for some reason no matter what I do the main activity is always destroyed when the notification is pressed. I've tried singleTop, singleTask (both as intent flags and manifest values), saving instance state bundles, onNewIntent, basically anything close to a solution I can find. But the activity is ALWAYS destroyed. I can only get the intent through getIntent.
Main Activity: https://pastebin.com/32uuK33E
Audio Service: https://pastebin.com/ShiUfBMc
Manifest: https://pastebin.com/kPp7RSYK
since "links to pastebin must be accompanied by code"
here's the intent (i've tried every combination of relevant flags so I really doubt that's the issue)
// creates an intent which opens the application when the notification is pressed
private PendingIntent getPendingIntent(Context context) {
Intent intent = getIntent(this);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(intent);
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
// returns an intent which resumes the Main Activity and passes various song information
private Intent getIntent(Context context) {
int duration = player != null ? player.getDuration() : 0;
boolean playing = player != null && player.isPlaying();
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO, getSongInfo());
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_POS, currentQueuePos);
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, duration);
intent.putExtra(MediaPlayerContract.INTENT_EXTRA_IS_PLAYING, playing);
return intent;
}
and here's where i'm trying to read the data
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e(LOG_TAG, "flag got intent");
String[] info = intent.getStringArrayExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO);
int pos = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_POS, 0);
int duration = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, 0);
unpackageSongInfo(info);
currentQueuePos = pos;
seekBar.setMax(duration);
setIntent(intent);
}
Solution
I've solved the problem. I needed to call the setSessionActivity() method on my media session, like so:
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, MediaPlayerContract.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSessionCompat.setSessionActivity(pendingIntent);
EDIT:
you can also create an intent by simply calling
setContentIntent(controller.getSessionActivity())
after calling setSessionActivity, where controller is your MediaSessionController.
Answered By - JonDal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.