在android中按通知时打开一个片段

时间:2014-06-25 08:51:27

标签: android android-fragments notifications

我在通知栏中按通知时尝试打开片段。 我的app结构是:

  • 带导航抽屉菜单的基本活动
  • 以及从菜单中打开的一些片段

当我按下通知时,活动会重新打开但不会显示在指定的片段中,而在LogCat中,指示片段中的数据会打开并加载,但不会显示在UI中。

通知代码:

       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);             
    } 

为什么活动打开但片段没有?

2 个答案:

答案 0 :(得分:0)

尝试以下代码,这是显示通知的更简单方法:

Intent resultIntent = new Intent(this, MainActivity.class);

resultIntent.setAction("Action1");

PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.icon_notification, "Your title", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, "Your title", "Your text", pendingIntent);

mNotificationManager.notify(notificationID, notification);

答案 1 :(得分:0)

播放广播

在通知类中,您必须发送广播。它的默认方法。

        `intent.setAction("one");
        sendBroadcast(intent);`

在Base Activity中创建Broadcast reveiver

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent!=null && intent.getAction().equals("one")) 

        {
            displayView(fragement_position);
        }
    }
};

在创建BaseAcitivty时,重建广播接收器

  IntentFilter filter = new IntentFilter();
    filter.addAction("one");
    registerReceiver(getList, filter);

您必须添加intentFilter

并且您必须取消注册基础活动的销毁

@Override protected void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); }