problems with Fragment onBackPress

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:55:03

问题


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

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 in my activity:

@Override
public void onBackPressed() {

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

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

Finally I found the solution!

in my fragment I added below code:

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

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

回答1:


used this method instead of onBackPressed

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>



回答2:


Try this:

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


来源:https://stackoverflow.com/questions/36664266/problems-with-fragment-onbackpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!