Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent new activity instance after clicking on notification

Tags:

application (non-wanted) behavior -

  1. application is started, some text is put into text-box and notification is created through button action.
  2. user "clicks" the home button, application is "minimized", notification is available in bar
  3. user selects the notification and the application is "maximized"

BUT - instead of the original instance, new instance is started (e.g. in the newest instance is missing the original text; when the latest instance is closed there is still the original instance with original text ) .

the code of the notification method

Context context = getApplicationContext();     CharSequence contentTitle = "someText1";     CharSequence contentText = "someText2";     Intent notifyIntent = new Intent(Intent.ACTION_MAIN);     notifyIntent.setClass(getApplicationContext(), RadioStream.class);     PendingIntent intent =         PendingIntent.getActivity(RadioStream.this, 0, notifyIntent, 0);      notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);     mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 

i have also in the manifest xml file following tag

android:launchMode="singleTask" 

but it is still the same... The main problem is double/triple initialization of the application, i know that there are other means to preserve the values in resumed applications. Also it is needed that the applications stays running in background as the main functionality is the streaming of internet radio.

What is missing in the code ? What kind of information from my side is missing for to troubleshoot the issue ?

Thanks!

Dav

like image 254
user375418 Avatar asked Jun 24 '10 15:06

user375418


People also ask

How do you handle opening activity using notifications?

Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .

Can we create instance of activity android?

Activity instances are always created by the Android system. This is because a lot of initializations have to be done for the activity to work. To create a new activity you call startActivity with an Intent describing the activity to start. To add more to this answer.

Which method is used to start an activity using intent instance?

An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity() .


2 Answers

What you refer as "application" is most probably an Activity. To avoid re-crating it when bringing up to front, use

android:launchMode="singleTop" 

To have some code running background, you need to realize it as Service

like image 91
ognian Avatar answered Oct 12 '22 09:10

ognian


Another option is to use setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP) on your Intent.

like image 42
CommonsWare Avatar answered Oct 12 '22 07:10

CommonsWare