3

2 つのアクティビティ A と B を想像してください。

  1. A がアクティビティ B を開始します (バックスタックの一番上になります)
  2. 保留中のインテントを作成し、新しいアクティビティ B に戻ることを許可する通知にそれを添付します。フラグ FLAG_ACTIVITY_REORDER_TO_FRONT は、1 つのアクティビティ B (ステップ 1 で作成したもの) を持つために設定されます。

  3. アプリを終了し、通知バーを開き、通知をクリックします。ステップ 1 で作成したアクティビティ B に戻ると思います。それは新しいアクティビティ B を作成し、バックスタックの一番上に置きます。何!?この行動が理解できない!

これに関する重要な詳細がいくつかあります (バグの匂いを検出できますか?):

  • ステップ 1 と 2 は、アクティビティ A が持つ Fragment で実行されます。

  • サポート v4 ライブラリを使用しており、アクティビティは SherlockFragmentActivity の拡張であり、ActionBar.TabListener を実装しています。

コードのスニペットを次に示します。

//Activity B creates and showed
Intent i = new Intent(getActivity(), B.class);
getActivity().startActivity(i);

Intent notificationIntent = new Intent(getActivity(), Prova2.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
notificationIntent.putExtra("test", "hi I'm a test");

PendingIntent contentIntent = PendingIntent.getActivity(getActivity(), 0, notificationIntent, 0);
String n = MyApplication.getAppContext().NOTIFICATION_SERVICE;
NotificationManager nm= (NotificationManager) getActivity().getSystemService(n);

Notification notification = new Notification(R.drawable.icon, "test", System.currentTimeMillis());
notification.setLatestEventInfo(MyApplication.getAppContext(), "testtitle", "texttest",
                    contentIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;

nm.notify(1, notification);
4

1 に答える 1