lakshman
lakshman

Reputation: 569

notification launches multiple instances of Activities

I am developing a media player app for playing the audio files in the SD card. By using the application, i can able to view the audio files, i am able to play the selected song. when the back button is pressed while playing the song, notification is created. The problem comes here, when i am launching the MainActivity (by using the PendingIntent), the application is launching new instance of the MainActivity rather than launching the previous MainActivity instance.

I made the MainActivity launchMode to "Singletop" in the manifest.FYI, the application is having only one activity.

I tried to add the notification flags also, but still the application is launching another instance.

what went wrong, i am unable to get. Please help me out.

Upvotes: 3

Views: 1378

Answers (2)

Rick77
Rick77

Reputation: 3221

Old question, but to integrate kishu27 solution (thank you!), which works but has the only drawback of resetting the user navigation within the app to the destination intent clearing the stack (which may or may not be what you want), another solution is to create a specialised activity with the only task of be the destination of the Intent:

public class NotificationLanding extends Activity {

    @Override
    public void onCreate(Bundle mainBundle) {
        super.onCreate(mainBundle);

        View view = new View(this);
        view.setBackgroundColor(Color.TRANSPARENT);
        setContentView(view);

        finish();
    }
}

(also don't forget to declare the activity it in the manifest!)

<activity android:name=".NotificationLanding"/>

and use it to just "focus" your application without impacting the navigation of your user. Perhaps there are other means to get the same effect, comments are welcome.

I would advise to also integrate kishu27 Intent flags in your Intent (I did), although it's not strictly necessary.

hope it will help

Upvotes: 0

kishu27
kishu27

Reputation: 3120

Intent intent=new Intent(this,Element.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

This worked for me :)

Upvotes: 2

Related Questions