Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open fragment page, when pressed a notification in 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)

like image 277
Venkat Ramarao Potlapalli Avatar asked Oct 28 '14 12:10

Venkat Ramarao Potlapalli


People also ask

How do you open a fragment in a fragment?

You should create a function inside activity to open new fragment and pass the activity reference to the fragment and on some event inside fragment call this function. From inside a fragment you automatically already have a reference to the parent activity.

Can fragment to activity in Android?

You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.


1 Answers

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)  ...  

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

like image 77
arsent Avatar answered Sep 22 '22 02:09

arsent