Android: How to avoid that clicking on a Notification calls onCreate()

后端 未结 3 1929
深忆病人
深忆病人 2020-12-28 16:52

In my application I notify the user with notifications, if something special happens:

public void triggerNotification(String msg) {
        notificationManag         


        
相关标签:
3条回答
  • 2020-12-28 17:32

    I've discovered that if you use Intent contentIntent = new Intent(this, ABC.class); this calls onCreate(); regardless of the flags set.

    Use Intent contentIntent = getIntent(); to skip onCreate(); and that moves to onStart();

    0 讨论(0)
  • The recommendation to use FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP only partially solves the problem. The activity in the Android manifest should also have these settings applied so that launching the activity from the home screen has the same behavior. Without these properties multiple instances of the activity can be launched.

    <activity android:name="foo"
              android:clearTaskOnLaunch="true"
              android:launchMode="singleTop"
              android:label="@string/app_name">
    
    0 讨论(0)
  • 2020-12-28 17:38

    To bring your app to the foreground if it is running already you need to set different flags on your intent:

    contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    For running a specific method you could just pass extra information along with the intent and interpret it in your application to decide which method to run.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题