Fragment onBackPress的问题

时间:2016-04-16 12:36:35

标签: android android-fragments android-activity navigation-drawer onbackpressed

在我的导航抽屉里有5个菜单项。其中一个是关于我们的项目。当我点击这个项目时,我调用AboutUsFragment并显示它(它的内容只是一个文本)。但是当我点击onBackPress时,片段已经消失,但它的文本仍然在我的活动上。我可以解决这个问题吗?它与它有什么关系?!

在我的活动中选择导航抽屉项目:

public void selectItem(int position) {

    Fragment fragment = null;

    switch (position) {
        case 0:
            if (!Constants.login_state) {
                fragment = new LoginFragment();
            } else {
                Logout();
            }
            break;

        case 1:
            Constants.filter = false;
            Constants.gender = "-1";
            fragment = new HomeFragment();
            break;

        case 2:
            Constants.filter = false;
            Constants.gender = "2";
            StyleFragment.SortingMode = 1;
            fragment = new StyleFragment();
            break;

        case 3:
            Constants.filter = false;
            Constants.gender = "1";
            StyleFragment.SortingMode = 1;
            fragment = new StyleFragment();
            break;

        case 4:
            fragment = new AboutUsFragment();
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
        fragmentManager.replace(R.id.rl_container, fragment);
        fragmentManager.addToBackStack(null);
        fragmentManager.commit();
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(Gravity.END);
    } else {
        Log.e("HomeActivity", "Error in creating fragment");
    }
}

和AboutUsFragment:

public class AboutUsFragment extends android.support.v4.app.Fragment{

private View view;
private TextView about_us_fragment_text_view;

public static AboutUsFragment newInstance() {
    AboutUsFragment fragment = new AboutUsFragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view=inflater.inflate(R.layout.fragment_about_us, container, false);
    Casting(view);
    about_us_fragment_text_view.setText(getResources().getString(R.string.about_us));
    ChangeUIFont.ChangeFont((ViewGroup) view, getContext());

    return view;
}


//casting parameters
public void Casting(View v){
    about_us_fragment_text_view= (TextView) v.findViewById(R.id.about_us_fragment_text_view);
}}
在我的活动中

onBackPress:

@Override
public void onBackPressed() {

    if (SearchOpened) {
        lv_searchResult.setVisibility(View.GONE);
        SearchOpened = false;
    } else
        super.onBackPressed();
}

--------------------------------------- AfterSearching ------- ------------------------------------

最后我找到了解决方案!

在我的片段中,我添加了以下代码:

public static final String FRAGMENT_NAME = AboutUsFragment.class.getName();

在我的活动中,我在调用时设置了片段的标记,而不是null!

fragmentManager.replace(R.id.rl_container, fragment,fragmentName);

2 个答案:

答案 0 :(得分:1)

试试这个:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
    } else {
      getFragmentManager().popBackStack();
    }

答案 1 :(得分:1)

使用此方法代替onBackPressed

如果您有工具栏,那么这是我的解决方案, 在工具栏下面的oncreate方法下输入

    assert getSupportActionBar() != null;
  //  getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

并在您的片段

的清单文件中设置此代码
    <activity android:name=".yourCurrentFragment">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".whichActivityYouWantToGo" />
    </activity>