片段onBackPress的问题 [英] problems with Fragment onBackPress

查看:92
本文介绍了片段onBackPress的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的导航抽屉中有5个菜单项.其中一个与我们有关已经不见了,但是它的文本仍然留在我的活动中.我该如何解决这个问题?它与什么有关?!

in my navigation drawer there are 5 menu items.one of them is about us item.when I click on this item, I call AboutUsFragment and show it(its content is just a text).but when I click onBackPress, fragment is gone but its texts remains on my activity.how can I solve this problem?what`s it related to?!

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

select item of navigation drawer in my activity:

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:

and 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我的活动中:

onBackPress in my activity:

@Override
public void onBackPressed() {

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

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

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

最后我找到了解决方案!

Finally I found the solution!

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

in my fragment I added below code:

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

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

and in my activity,I set the tag of fragment when I call it,instead of null!

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

推荐答案

使用此方法代替onBackPressed

used this method instead of onBackPressed

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

if you have toolbar then here is my solution, type under the oncreate method below toolbar,

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

}

并在清单文件中为您的片段设置此代码

and set this code in your manifest file for your fragment

    <activity android:name=".yourCurrentFragment">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".whichActivityYouWantToGo" />
    </activity>

这篇关于片段onBackPress的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆