我試圖在通知欄中按通知時打開一個片段。 我的應用程序的結構是:在android中按通知時打開一個片段
- 與導航抽屜菜單
- 和一些片段被從菜單
開了一個基本活動。當我按通知的活動重新開放,但不是在指示的片段,在LogCat中,來自指定片段的數據看起來是打開並加載的,但不是用戶界面。
通知碼:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Comanda Noua");
mBuilder.setContentText("S-a introdus o comanda noua");
mBuilder.setTicker("Comanda noua!");
mBuilder.setSmallIcon(R.drawable.calculator_icon);
mBuilder.setAutoCancel(true);//inchide notificare dupa ce s-a dat click pe ea
//creste numar notificare
mBuilder.setNumber(++numMessages);
// cream un intent
Intent resultIntent = new Intent(this, MainActivity.class);
//am setat actiune pentru a deschide fragmentul corespunzator notificartii la apasare
resultIntent.setAction("Action1");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());
我在哪裏開的片段時,按下通知代碼:
Intent intent = getIntent();
try{
String action = intent.getAction();
// Log.d("action:",action);
if(action.equals("Action1")){
//Log.d("deschidem agenda:",action);
AgendaFragment fragment = new AgendaFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null).commit();
}else{
Log.d("eroare", "Intent was null");
}
}catch(Exception e){
Log.e("eroare", "Problem consuming action from intent", e);
}
爲什麼活動開放,但片段沒有?
爲什麼您使用TaskStackBuilder創建掛起的意圖? –
我是Android新手,通知代碼來自互聯網。我想我使用它來把活動放在stack.if我沒有使用通知什麼都不做 – user3481497