如何在android中按下通知时打开片段页面

Ven*_*lli 31 android android-notifications android-fragments android-pendingintent

我在通知栏中按通知时试图打开一个片段.我的app结构是:

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

    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);
    
    Run Code Online (Sandbox Code Playgroud)

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

Vit*_*ito 36

您将需要像往常一样启动基本活动,但添加一些额外信息以了解将打开哪个菜单片段的意图.在这里,您可以看到它是如何完成的:https://stackoverflow.com/a/8610916/1652236

这取决于您在用于启动/加载片段的活动'onCreate()'方法中检索的额外信息.

请参阅此处,例如如何使用片段:http ://www.tutorialspoint.com/android/android_fragments.htm http://developer.android.com/guide/components/fragments.html

它意图启动此过程将类似于:

Intent notificationIntent = new Intent(getActivity(), Abc.class);
notificationIntent.putExtra("menuFragment", "favoritesMenuItem");
Run Code Online (Sandbox Code Playgroud)

在您的基础活动中:

@Override
protected void onCreate(final Bundle savedInstanceState)
{
    String menuFragment = getIntent().getStringExtra("menuFragment");

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    // If menuFragment is defined, then this activity was launched with a fragment selection
    if (menuFragment != null) {

        // Here we can decide what do to -- perhaps load other parameters from the intent extras such as IDs, etc
        if (menuFragment.equals("favoritesMenuItem")) {
            FavoritesFragment favoritesFragment = new FavoritesFragment();
            fragmentTransaction.replace(android.R.id.content, favoritesFragment);
         }
    } else {
         // Activity was not launched with a menuFragment selected -- continue as if this activity was opened from a launcher (for example)
         StandardFragment standardFragment = new StandardFragment();
         fragmentTransaction.replace(android.R.id.content, standardFragment);
    }
}
Run Code Online (Sandbox Code Playgroud)


ars*_*ent 11

欢迎使用2019和导航组件:)

如果使用的是NavigationComponent,则可以使用NavDeepLinkBuilder以下命令打开特定的目标:


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

...

notificationBuilder.setContentIntent(pendingIntent)

...

Run Code Online (Sandbox Code Playgroud)

请注意,setComponentName仅当目的地不在启动器活动中时使用才重要。

  • 请注意,这将打开该活动的一个新实例。我花了几个小时调试这个。还有其他方法可以打开现有活动,但我不确定 NavDeepLinkBuilder 是正确的方法 (4认同)
  • 我无法用这种灵魂重新创建后退堆栈。当我使用此解决方案时,我有片段 A -> B -> C -> D -> E ,图形只有 A -> E 。任何想法? (3认同)

小智 8

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
             | Intent.FLAG_ACTIVITY_SINGLE_TOP )
Run Code Online (Sandbox Code Playgroud)

由于你的意图设置Flags:FLAG_ACTIVITY_SINGLE_TOP,在创建活动时不会调用"onCreate()",你应该在名为"onNewIntent()"的方法中接收params.