How to open fragment page, when pressed a notification in android

Solution 1:

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.

Solution 2:

You will need to start your base activity as usual, but add some extra info to the intent about what menu fragment will be opened. Here you can see how it can be done: https://stackoverflow.com/a/8610916/1652236

This depends on the extra information which you retrieve in the activities 'onCreate()' method in which you will use to start/load the fragment.

See here for example how work with fragments: http://www.tutorialspoint.com/android/android_fragments.htm http://developer.android.com/guide/components/fragments.html

It Intent to launch this procedure will be something like:

Intent notificationIntent = new Intent(getActivity(), Abc.class);
notificationIntent.putExtra("menuFragment", "favoritesMenuItem");

and in your base activity:

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

Solution 3:

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

As your intent set Flags: FLAG_ACTIVITY_SINGLE_TOP, "onCreate()" will not be called when the activity has been created, you should receive the params in the method called "onNewIntent()" instead.

Solution 4:

you should also add .commit(); and ft1.addToBackStack(null); so that it would not overlap on prevoius one and if you will not ad this ft1.addToBackStack(null); on back your app will exit so add this according to your functionality

String menuFragment = getIntent().getStringExtra("menuFragment");

ft1 = getSupportFragmentManager().beginTransaction();

ft1.addToBackStack(null);

ft1.replace(R.id.frame_container, favoritesFragment).commit();