在android中按下通知时如何打开片段页面 [英] How to open fragment page, when pressed a notification in android

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

问题描述

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

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

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

  • a base activity with a nav drawer menu
  • some fragment that are opened from menu

b.setOnClickListener(new OnClickListener() {

        @SuppressWarnings({ "deprecation", "static-access" })
        public void onClick(View v) {

        w_nm=(NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);

         Notification notify=new Notification(R.drawable.notnificationlogo,waternoti,System.currentTimeMillis());

         Intent notificationIntent = new Intent(getActivity(), Abc.class);



         PendingIntent pending=PendingIntent.getActivity(getActivity(), 0,notificationIntent, 0);


         notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                 | Intent.FLAG_ACTIVITY_SINGLE_TOP );

        notify.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

           notify.setLatestEventInfo(getActivity(),waternoti,waternoti1, pending);

         w_nm.notify(0, notify);

谁能告诉我如何链接到下一个片段页面(当前代码在扩展片段的类中)

Can anyone tell me how to link with next fragment page (the present code is in class that extends fragment)

推荐答案

如果您使用的是 Navigation 组件,您可以使用 NavDeepLinkBuilder 打开特定目的地:

If you are using Navigation Component you can open a specific destination using NavDeepLinkBuilder:


val pendingIntent = NavDeepLinkBuilder(context)
                     .setComponentName(MainActivity::class.java)
                     .setGraph(R.navigation.nav_graph)
                     .setDestination(R.id.destination)
                     .setArguments(bundle)
                     .createPendingIntent()

...

notificationBuilder.setContentIntent(pendingIntent)

...

请注意,只有当您的目的地不在启动器活动中时,才使用 setComponentName 很重要.

Please note that it's important to use setComponentName only if your destination isn't in the launcher activity.

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

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