How to use FLAG_ACTIVITY_SINGLE_TOP and several tasks with PendingIntents

I am developing a multi-chat application for Android based on SocketIO. This is my first Android app, so maybe I'm missing something stupid. The structure of the application is basically activity for the home screen and activity that connects to the SocketIO server and offers a chat interface.

The application provides an intent filter for opening special matching links to launch the chat application with the filled "room" field. When I open one of these "special links", the application starts with a new task, so I can start 2 chat sessions at the same time.

Intent Filter Code

<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" android:host="mychat.example.com" android:pathPrefix="/mychat/room/" /> </intent-filter> 

Everything works fine regardless of notifications: each notification has a PendingIntent associated to resume a chat session if new messages appear. For renewal I use Intent.FLAG_ACTIVITY_SINGLE_TOP. Here is the code to resume the application

 Intent intent = new Intent(getApplicationContext(), Room.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); mNotifyBuilder.setContentText(message.toString()).setNumber(++notificationMessages); Notification note = mNotifyBuilder.setContentIntent(pIntent).build(); note.flags |= Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(notificationID, note); 

If I use the application from the launchpad, everything works fine, but with a task launched from the intent filter, an error appears with a notification. If I receive a message from a task from an intent filter, and I click on a notification, the application from the launcher resumes instead of one of the intent filter.

Any clues?

Thanks!

+4
source share

Source: https://habr.com/ru/post/1481017/


All Articles