Home > front end >  Confused with launchMode
Confused with launchMode

Time:09-16

I made a Music Player that operates in two ways:

  1. If started as a normal app, it continues playing what it was playing when last closed, and it should start in the background;
  2. If an audio file is selected in a fileManager, it plays the song clicked in the foreground.

Since I want only one instance of the app running, so I added to the mainfest:

android:launchMode="singleTask"

What I did to accomplish this was to execute this onCreate:

    uri = getIntent.getData();
    if (uri != null) {
        newsong = uri.toString()...
        play(newsong);
    } else {
        moveTaskToBack(true);
        playoldsong();
    }

It works great, except for the fact that, when started as in (1) and, after a while, I select a song in a filemanager, the main activity is brought to the front (which is what I want) BUT it continues playing what it was playing before the song selection. The player ignored the new song, and I want it to switch to it.

This actually makes sense, since the only place where I call the new song is in the onCreate.

Maybe the launchMode is not the correct one? Maybe I should @Overwrite the OnNewTask(Intent) method? Which is the 'cheaper' solution?

CodePudding user response:

You have to override onNewIntent() to start the song the user clicked on in the file manager.

  • Related