PendingIntent不会启动所需的片段

时间:2017-02-04 11:58:14

标签: android android-fragments android-intent android-pendingintent

我试图在同一个活动中使用PendingIntent。如果MainActivity已在运行且应用收到通知,则单击此通知时,应加载TestFragment类,但此处不是这种情况,单击通知时没有任何反应。

    Intent intent = new Intent(mContext, MainActivity.class);
    intent.putExtra("NOTIFICATION", "notify");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(mContext)
         .setContentIntent(pendingIntent)
         .setContentTitle(title)
         .setSmallIcon(R.drawable.logo)
         .setLargeIcon(bitmap)
         .setPriority(Notification.PRIORITY_MAX)
         .build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(1, notification);

但是当我将此代码用于IntentPendingIntent时,我会加载TestFragment但是它会创建两次活动。它将TestFragment加载到默认视图之上。

   Intent intent = new Intent(mContext, MainActivity.class);
   intent.putExtra("NOTIFICATION", "notify");
   PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

MainActivity.class onCreate()

String notificationIntent = getIntent().getStringExtra("NOTIFICATION");

   if(notificationIntent != null) {
       if (notificationIntent.equals("notify")) {
           TestFragment fr = new TestFragment();
           FragmentManager fm = getFragmentManager();
           FragmentTransaction fragmentTransaction = fm.beginTransaction();
           fragmentTransaction.replace(R.id.frame_container, fr);
           fragmentTransaction.commit();               
       }
   } else {
     //default view
   }

1 个答案:

答案 0 :(得分:1)

传递额外

的常量
   private static final String NOTIFICATION = "notification";

通知意图

    Intent intent = new Intent(mContext, MainActivity.class);
    intent.putExtra(NOTIFICATION, true);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = 
          PendingIntent.getActivity(mContext, 100, intent, 
                        PendingIntent.FLAG_UPDATE_CURRENT);

在MainActivity.class中 添加onNewIntent()

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

和onResume()

@Override
protected void onResume() {
    super.onResume();

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        final boolean fromNotification = extras.getBoolean(NOTIFICATION);
        if (fromNotification) {
            TestFragment fr = new TestFragment();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.frame_container, fr);
            fragmentTransaction.commit();  
        }
    }
}

的AndroidManifest.xml

<activity
    android:name="test.com.sample.MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTop">

    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>