2016-05-21 91 views
0

我正在打開點擊通知的活動。但是如果該活動已經開放,那麼我想重新開放該活動。從重新打開我的意思是說;關閉該活動並再次打開以顯示更新的數據。換做是我加入這行代碼的通知的意圖:如何重新打開點擊/點擊通知的活動

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

但問題是,當我點擊/ TAP通知,整個應用程序被關閉,而不是隻完成該活動。所以請幫助。

+0

您可以通過定義機器人重複使用相同的活動:launchmode =「singleTop」或「singleInstance」清單文件的標籤,這樣就可以利用現有的情況下,如果它已經打開 –

回答

1

您可以檢查是否Activity已經打開。我做這樣

public static boolean FLAG=false; 
onCreate

使其true

@Override 
protected void onCreate(Bundle savedInstanceState) { 
FLAG=true; 
. 
. 
} 

現在你可以在Notification類檢查這個變量的值。

boolean isOpen=YourActivty.FLAG; 
if(isOpen){ 
//just clear the notification with adding the notification's data in the activity if you are sending to any listview or so. 
//for adding the data to listview you need to make the `ArrayList`(i assume) and make a method in the adapter that will notify the adapter. 
}else{ 
//call the Activity 
} 
+0

這是件好事。但我想在點擊通知後進行更改 –

+0

需要進行哪些更改 –

+0

我想突出顯示列表視圖的項目 –

0

使用此

  notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = null; 
     notificationIntent = new Intent(context, SplashScreen.class); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_NEW_TASK 
       | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 
     RemoteViews notiview = new RemoteViews(getPackageName(), 
       R.layout.notification_layout); 
     final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       context).setSmallIcon(small_icon) 
     // .setContentTitle(title) 
       .setAutoCancel(true) 
       // .setStyle(
       // new NotificationCompat.BigTextStyle().bigText(message)) 
       .setDefaults(
         Notification.DEFAULT_SOUND 
           | Notification.DEFAULT_VIBRATE) 
       // .setContentText(message).setNumber(noticount); 
       .setContent(notiview); 
     notiview.setTextViewText(R.id.noti_msgcount, "" + noticount); 
     notiview.setTextViewText(R.id.noti_msgtxt, msgOld); 

     mBuilder.setContentIntent(intent);