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
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
addPopBackStackListener();
}
Here is my addPopBackStackListener() function,
private void addPopBackStackListener() {
getSupportFragmentManager().addOnBackStackChangedListener(
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....
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
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
addPopBackStackListener();
}
Here is my addPopBackStackListener() function,
private void addPopBackStackListener() {
getSupportFragmentManager().addOnBackStackChangedListener(
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....
Over Under Monday Night Football Betting Offers & Tips matchpoint matchpoint betway betway クイーンカジノ クイーンカジノ 872Hard Rock Cherokee Casino & Resort in Bakersfield
ReplyDelete