如何知道任何片段是活动的还是可见的?

时间:2017-07-27 09:35:36

标签: java android android-fragments visible fragment-backstack

我在一个活动中有三个片段A,B和c,活动有一个带有测试视图标题的工具栏。

现在,当我从片段导航到片段时,我想根据显示的片段更改文本视图的文本。

对于片段B和C中的这个,我得到主要活动的工具栏和文本视图,并改变它的标题,如下所示:

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

这很好用。但是当我回到主片段时,我想再次更改标题,但它没有改变。

我尝试在主要活动的onCreate方法中设置它,并且像这样:

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

        }
    }
}

但是,即使B片段可见,它也会将其标题更改为主片段标题。

我该怎么做?请帮忙。谢谢。

编辑:

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

片段:

                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片段

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

2 个答案:

答案 0 :(得分:1)

此功能可让您知道片段是否可见

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

但在您的使用案例中,如果您使用ViewPagerTab,则可以通过此轻松获得您的标题

    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) {

        }
    });

答案 1 :(得分:1)

如果要更改不同片段的活动中工具栏的标题,请使用interface

创建界面:

public interface OnFragmentTitleChangeListener {
    void onFragmentTitle(String strTitle);
}

在您的活动中实施此界面

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;
  }
}

在片段的onResume方法之后设置文本。

@Override
public void onResume() {
    super.onResume();
    ((OnFragmentTitleChangeListener) mContext).onFragmentTitle(getResources().getString(R.string.yourtext));
}