How to open fragment page, when pressed a notifica

2019-01-21 20:02发布

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)

3条回答
啃猪蹄的小仙女
2楼-- · 2019-01-21 20:03

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);
    }
}
查看更多
混吃等死
3楼-- · 2019-01-21 20:04

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();
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-21 20:05
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.

查看更多
登录 后发表回答