3

I have three fragments A,B and c in one activity, activity has a toolbar with test view title.

Now as I navigate from fragment to fragment I want to change the text of text view depending on which fragment is shown.

For this in fragment B and C I am getting the toolbar of main activity and the text view and changing 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 fine. But when I go back to main fragment I want to change the title again but its not getting change.

I tried to set it in onCreate method of main activity, and 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 it changes its title to main fragments title even B fragment is visible.

How can I do this. Please help. Thank you.

EDIT:

MainFragment :

        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();

A 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();
Sid
  • 2,792
  • 9
  • 55
  • 111

3 Answers3

2

If you want to change title of toolbar in activity for different fragments than use interface.

Create 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 text from fragment's onResume method.

@Override
public void onResume() {
    super.onResume();
    ((OnFragmentTitleChangeListener) mContext).onFragmentTitle(getResources().getString(R.string.yourtext));
}
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
1

This function let you know if the fragment is visible or not

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // Handle when fragment is visible
    } else {
        // And not
    }
}

But in your use-case, if you using ViewPager or Tab, you can get your title easily by this

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            yourFragmentPagerAdapter.getTitle(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
phatnhse
  • 3,870
  • 2
  • 20
  • 29
0

use navigation graph for fragment navigations. By doing so simply you can get fragments label by calling navController.currentDestination.label

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '22 at 08:56