April 04, 2019

How to get Notification on Back Stack Change On android

            On this blog I am going to explain How to we can get notification on the fragment change on our Activity Class. In Some Cases we need to get the notification when the fragment is changed. We are using OnBackStackChangedListener for this. Here is the way we are doing it,

First we need to add the Listener on onCreate method of Activity Class . For this I have Created a function addPopBackStackListener(). Here is the way we initializing the  Listener

    protected void onCreate(Bundle savedInstanceState) {


Here is my addPopBackStackListener() function,

   private void addPopBackStackListener() {
            new FragmentManager.OnBackStackChangedListener() {
                public void onBackStackChanged() {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    if (fragmentManager != null && fragmentManager.getFragments() != null) {                   
                    int count = fragmentManager.getFragments().size();
                        if (count > 0) {
                            Fragment fragment = fragmentManager.getFragments().get(count- 1);
                            Log.v(TAG, "Class: " + fragment.getClass().getSimpleName());

                            /* Using instanceof keyword you can find out which fragment is loaded*/
                            if (fragment instanceof HomeFragment) {
                                 /*Add your code to Perform Action when Home Fragment is Loaded*/  


Thanks For Reading, Wish you a Happy Coding....

1 comment:

  1. Over Under Monday Night Football Betting Offers & Tips matchpoint matchpoint betway betway クイーンカジノ クイーンカジノ 872Hard Rock Cherokee Casino & Resort in Bakersfield