在Android中按通知时打开片段 [英] Open a fragment when press a notification in android

查看:46
本文介绍了在Android中按通知时打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按通知栏中的通知时试图打开一个片段.我的应用程序结构为:

I am trying to open a fragment when I press a notification in the notification bar. My app structure is:

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

当我按下通知时,活动将重新打开,但不会在指示的片段中打开,并且在LogCat中,指示的片段中的数据看起来会打开并加载,但没有使用UI.

When I press the notification the activity reopens but not in the indicated fragment, and in the LogCat the data from the indicated fragment looks to open and load but not with the 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());

按下通知时打开片段的代码:

The code where I open the fragment when notification is pressed:

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

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

Why the activity open but the fragment not?

推荐答案

尝试广播

在通知类中,您必须发送广播.其默认方法.

In notification class you have to sendBroadcast. its default method.

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

在基本活动中,只需创建广播接收器

In Base Activity just create 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收银机时,广播接收器

In on create of BaseAcitivty resiger the broatcast receiver

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

您必须添加intentFilter

You must add intentFilter

,您必须在销毁基本活动上取消注册

and you must unRegister in on Destroy of base Activity

@Override受保护的void onDestroy(){unregisterReceiver(receiver);super.onDestroy();}

这篇关于在Android中按通知时打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆