0

クリックするとアプリケーションにメッセージが表示され、ユーザーが戻るボタンをクリックするとメイン画面に戻るという通知を作成しようとしています。

問題は、現在、新しいインスタンスが作成され、以前のデータがすべて失われることです。私は何を間違っていますか?

 NotificationManager nm = (NotificationManager) getSystemService("notification");
 Intent intent = new Intent(this, beerwarn.class);
 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
 stackBuilder.addParentStack(beerwarn.class);
 stackBuilder.addNextIntent(intent);
 PendingIntent pIntent = stackBuilder.getPendingIntent
                                          (0, PendingIntent.FLAG_UPDATE_CURRENT);
 NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 builder.setContentIntent(pIntent)
        //Notification noti = new Notification.Builder(this)
        .setContentTitle("BAC Level Notice")
        .setContentText("Your BAC has dropped below Max Legal BAC")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pIntent)
        .setSmallIcon(R.drawable.beerwarn);
       //builder.flags |= Notification.FLAG_AUTO_CANCEL;

 nm.notify(1, builder.build());
4

2 に答える 2

0

これが私の提案です。メッセージ画面を表示したら、他のすべてのアクティビティをクリアしてください。

NotificationManager nm = (NotificationManager) getSystemService("notification");
Intent intent = new Intent(this, beerwarn.class);
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pIntent)
    .setContentTitle("BAC Level Notice")
    .setContentText("Your BAC has dropped below Max Legal BAC")
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent)
    .setSmallIcon(R.drawable.beerwarn);

nm.notify(1, builder.build());

beerwarn.class で onBackPressed 関数をオーバーライドし、MainScreen.class という名前であると仮定して、メインスクリーン アクティビティが実行されているかどうかを確認し、実行されていない場合は起動します。

ActivityManager mngr = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);
for (ActivityManager.RunningTaskInfo rti : taskList) {
    if (rti.topActivity.getClassName().equals(this.getClass().getName())) {
        if (!rti.baseActivity.getClassName().equals(MainScreen.class.getName())) {
            Intent i = new Intent(this, MainScreen.class);
            i.putExtra("currentTab", getIntent().getIntExtra("currentTab", 1));
            startActivity(i);
            break;
        }
    }
}

super.onBackPressed();
于 2013-05-02T03:43:24.517 に答える