2016-09-06 25 views
0

私は最近、自分のアプリケーションの通知を実装しました。私がテストしてきたのは、アプリケーションの動作方法です。アプリ通知からナビゲートしてアクティビティを再作成するにはどうすればよいですか?

インテント - >MainActivity.class - >戻る(アプリケーションを終了し、Androidデバイスのホーム画面に移動) - >タスクマネージャ - >アプリケーションを再度開く - >を開いて開きます。 Comments.class

最初のいくつかのステップは動作しますが、アプリケーションマネージャを開いてアプリケーションを開くと、Comments.classに行き着くことがわかりました。これは他のAndroidアプリケーションでテストしたように思われます。

しかし、私のComments.classのプロパティのいくつかは設定されておらず、それはComments.classonCreateメソッドが呼び出されていないためです。これが起こらない理由がありますか?

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.javaonCreate関数が呼び出され、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の詳細は初期化されていません。なぜこれが起こっているのか?

ご協力いただければ幸いです。ありがとう!

答えて

0

onResume内に初期化を入れてComment.javaの機能を入れます。ここでは、正しく動作を理解するのに役立つかもしれないAndroid Activity Lifecycleについての良い読書です。

@Override 
public void onResume() { 
    // Your initialization 
} 
関連する問題