How do I know if any fragment is active or visible?
I have three fragments A, B, and C in an activity, and the activity has a toolbar with a test View title.
Now, when I navigate from one fragment to another, I want to change the text of the TextView depending on the fragment displayed.
To do this, in fragments B and C, I took the toolbar and TextView of the main activity and changed its title like this:
final Toolbar toolbar = (Toolbar) ((MainActivity) getActivity()).findViewById(R.id.toolbar);
((MainActivity) getActivity()).setSupportActionBar(toolbar);
TextView title = (TextView) getActivity().findViewById(R.id.textView_Title);
title.setVisibility(View.VISIBLE);
title.setText(R.string.profile);
This works great. But when I go back to the main fragment, I want to change the title again, but it doesn’t get changed.
I tried to set it in the onCreate method of the main activity, like this:
@Override
public void onBackPressed() {
DashboardFragment test = (DashboardFragment) getSupportFragmentManager().findFragmentByTag("DASHBOARD_FRAGMENT");
if (test != null && test.isVisible()) {
DO STUFF
title = (TextView) findViewById(R.id.textView_Title);
title.setVisibility(View.VISIBLE);
title.setText(R.string.dashboardTitle);
}
else {
Whatever
}
Do nothing if the back button is disabled.
if (!mBackPressCancelled) {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStackImmediate();
}
else {
super.onBackPressed();
}
}
}
But with this, even if B fragment is visible, it will change its title to the main fragment title.
How do I do it. Please help. Thank you.
Edit:
Main fragment :
fragmentManager = getSupportFragmentManager();
DashboardFragment fragment1 = new DashboardFragment();
Bundle bundle = new Bundle();
fragment1.setArguments(bundle);
fragmentManager.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.frame, fragment1, "DASHBOARD_FRAGMENT").commitAllowingStateLoss();
fragment :
fragmentManager = ((MainActivity)(mContext)).getSupportFragmentManager();
ProfileFragment fragment1 = new ProfileFragment();
Bundle bundle = new Bundle();
fragment1.setArguments(bundle);
fragmentManager.beginTransaction().add(R.id.frame, fragment1, "PROFILE_FRAGMENT").addToBackStack("B").commit();
B fragment
fragmentManager = getActivity().getSupportFragmentManager();
ProfileEditFragment fragment1 = new ProfileEditFragment();
Bundle bundle = new Bundle();
fragment1.setArguments(bundle);
fragmentManager.beginTransaction().add(R.id.frame, fragment1, "PROFILE_EDIT_FRAGMENT").addToBackStack("C").commit();
Solution
If you want to change the title of the Activity toolbar for a different fragment, instead of using interface
.
Create an interface:
public interface OnFragmentTitleChangeListener {
void onFragmentTitle(String strTitle);
}
Implement this interface in your activity
public class YourActivity extends AppCompatActivity implements OnFragmentTitleChangeListener
{
TextView title;
......
......
Initialize title in onCreate method
......
......
Override this method
public void onFragmentTitle(String strTitle)
{
title.setText(strTitle);
}
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (onBack(fm)) {
return;
}
super.onBackPressed();
}
private boolean onBack(FragmentManager fm) {
if (fm != null) {
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
return true;
}
}
return false;
}
}
After that, set the text from the fragment’s onResume method.
@Override
public void onResume() {
super.onResume();
((OnFragmentTitleChangeListener) mContext).onFragmentTitle(getResources().getString(R.string.yourtext));
}