Android:如果在屏幕上重新启动活动,来自通知的PendingIntent不会触发onCreate()

lon*_*ngi 8 android android-intent android-notifications android-pendingintent

猜测我对Intent Flags有一些误解.我正在尝试做的是,我有一个无线电流应用程序,它有两个活动(PlayerApplication和SettingsScreen).我有一个用于Streaming的Service.class,它在后台运行,它也包含一个Notification(你可以在notification-overlay菜单和PlayerApplication中停止/开始播放).如果用户单击通知,则应将PlayerApplicationActivity返回到屏幕.

一切正常,期待案例:用户打开SettingsScreenActivity - >打开NotificationOverlayMenu - >点击通知 - > PendingIntent恢复到PlayerApplicationActivity

现在,PlayerApplicationScreen就在那里,但我无法点击任何内容.我看,如果我从Notification继续,我的PlayerApplication的onCreate()不会被触发.但布局看起来很好(在onCreate()中也膨胀)

我在Service.class中使用了以下PendingIntent:

PendingIntent contentIntent = PendingIntent.getActivity(
      this.getApplicationContext(), //Service.class
      0, 
      new Intent(this,PlayerApplicationActivity.class)
             .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP),
      0);
Run Code Online (Sandbox Code Playgroud)

所以实际上PendingIntent应该将Activity(如果已经运行)带回到顶部.我使用错误的意图还是我错过了一点?

编辑:我在Manifest.xml中声明了launchMode

android:launchMode="singleTask"
Run Code Online (Sandbox Code Playgroud)

and*_*guy 7

据Android开发者网站,在FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP在启动模式:SingleTask,如果要调用已在运行,然后,而不是创建新实例活动的实例,它调用onNewIntent()方法.所以我认为你应该在onCreate()没有调用时检查你的活动是否正在运行.


lon*_*ngi 1

我刚刚发现我做错了什么:

  1. 为了我的目的,我使用了错误的启动模式。正确的一个singleTop不是singleTask

  2. 我在中声明了 launchMode<application-node>而不是<activity-node>

    //Manifest.xml
    <activity
        android:name="..."
        android:launchMode="singleTop" >
    
    Run Code Online (Sandbox Code Playgroud)