Android - How to do nothing when tap on notification app and the app is already opened -
i have notification in app , want when tap on notification:
- if app opened: nothing;
- if app closed: open app;
current code:
intent intent = new intent(); intent.setclass(mcontext, mainactivity.class); intent.addflags(intent.flag_activity_clear_task); pendingintent contentintent = pendingintent.getactivity(mcontext, 0, intent, pendingintent.flag_update_current); notificationcompat.builder mbuilder = new notificationcompat.builder(mcontext); mbuilder.setsmallicon(icon); mbuilder.setcontenttitle(string); mbuilder.setwhen(system.currenttimemillis()); mbuilder.setongoing(true); mbuilder.setcontentintent(contentintent); mbuilder.setvisibility(notificationcompat.visibility_public); mbuilder.setcolor(mcontext.getresources().getcolor(r.color.theme_color)); notification notification = mbuilder.build(); mnotificationmgr.notify(id, notification);
activity manifest:
<activity android:name=".mainactivity" android:configchanges="keyboardhidden|orientation|mcc|mnc" android:launchmode="singletop" android:screenorientation="portrait" android:label="@string/app_name" <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity>
if tap on notification , app opened, opens app in front of own app! if app closed, ok!
i'd replace line:
intent.addflags(intent.flag_activity_clear_task);
with following:
intent.addflags(intent.flag_activity_single_top | intent.flag_activity_reorder_to_front);
from documentation:
public static final int flag_activity_single_top
if set, activity not launched if running @ top of history stack.
public static final int flag_activity_reorder_to_front
if set in intent passed context.startactivity(), flag cause launched activity brought front of task's history stack if running.
for example, consider task consisting of 4 activities: a, b, c, d. if d calls startactivity() intent resolves component of activity b, b brought front of history stack, resulting order: a, c, d, b. flag ignored if flag_activity_clear_top specified.
singlet_top prevent activity being launched again , reorder_to_front prevents being recreated. hope helps.
Comments
Post a Comment