私は最近、自分のアプリケーションの通知を実装しました。私がテストしてきたのは、アプリケーションの動作方法です。アプリ通知からナビゲートしてアクティビティを再作成するにはどうすればよいですか?
インテント - >MainActivity.class
- >戻る(アプリケーションを終了し、Androidデバイスのホーム画面に移動) - >タスクマネージャ - >アプリケーションを再度開く - >を開いて開きます。 Comments.class
最初のいくつかのステップは動作しますが、アプリケーションマネージャを開いてアプリケーションを開くと、Comments.class
に行き着くことがわかりました。これは他のAndroidアプリケーションでテストしたように思われます。
しかし、私のComments.class
のプロパティのいくつかは設定されておらず、それはComments.class
のonCreate
メソッドが呼び出されていないためです。これが起こらない理由がありますか?
NotificationService.java
:これは私の試みであった
Intent resultIntent = new Intent(context, Comments.class);
Intent backIntent = new Intent(context, MainActivity.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Set a random notification Id for each notification so that if you get multiple, the first does not get replaced.
Random random = new Random();
int notificationId = random.nextInt(9999 - 1000) + 1000;
PendingIntent pendingIntent = PendingIntent.getActivities(context, notificationId, new Intent[] { backIntent, resultIntent}, PendingIntent.FLAG_ONE_SHOT);
//When the notification is actually clicked on
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notificationBuilder.build());
この最初の通知を作成し、アプリケーションをユーザに通知します。その後Comments.java
のonCreate
関数が呼び出され、Notification
をクリックすると期待されます。その後、私は単純にそうようにバックMainActivity
にナビゲートしているバッククリック:
Comment.java
:
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
その後、MainActivity.java
は、すべての罰金であると呼ばれ、作成されます。私のデバイスのホーム画面に戻って私を取るMainActivity.java
@Override
public void onBackPressed() {
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
から
ここにあります:私はMainActivity.java
に戻ってクリックすると、私は次のコードを実行します。しかし、アプリケーションのインスタンスはバックグラウンドで実行されています。アプリケーションをバックグラウンドでクリックしてフォアグラウンドに置くと、Comments.java
に戻りますが、Comments.java
のメソッドonCreate
が呼び出されないため、 Activity
の詳細は初期化されていません。なぜこれが起こっているのか?
ご協力いただければ幸いです。ありがとう!