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

时间:2013-03-05 16:27:52

标签: 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);

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

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

android:launchMode="singleTask"

2 个答案:

答案 0 :(得分:6)

根据Android开发人员site,在FLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_SINGLE_TOP和启动模式下:SingleTask,如果您正在调用的活动实例已在运行,则代替创建新实例时,它调用onNewIntent()方法。所以我认为你应该在onCreate()没有调用的时候检查你的活动是否正在运行。

答案 1 :(得分:1)

我只知道我做错了什么:

  1. 出于我的目的,我使用了错误的launchMode。正确的是singleTop而非singleTask

  2. 我在<application-node>而不是<activity-node>

    中声明了launchMode
    //Manifest.xml
    <activity
        android:name="..."
        android:launchMode="singleTop" >
    
相关问题