backPress如何在片段中工作?

时间:2015-10-19 05:16:43

标签: android fragment android-fragmentactivity

我正在尝试在片段上使用反印。我无法解决它。以下是我的代码。

 /**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    if (position!=3){
        pos = position;
    }
    switch (position) {
        case 0:
            fragment = new Profile();
            break;
        case 1:
            fragment = new Products();
            break;
        case 2:
            fragment = new Help();
            break;
        case 3:
            DialogLogout(DrawerFragment.this, getString(R.string.logout), getString(R.string.cofirm_logout));

            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment)
                .commit();
        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(pos, true);
        mDrawerList.setSelection(pos);
        setTitle(navMenuTitles[pos]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

在产品碎片上我有一个列表,我再次使用它来调用另一个碎片。

 listCards.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Fragment fragment = new Transactions();

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                     //   .replace(R.id.frame_container, fragment)
                        .remove(Products.this)
                        .add(R.id.frame_container, fragment) //replace(R.id.frame_container, fragment)
                        .addToBackStack(null)
                        .commit();
                } else {
                // error in creating fragment
                Log.e("MainActivity", "Error in creating fragment");
            }
        }
    });
    return rootView;
} 

DrawerFragmentActivity上的BackPress功能如下所示:

@Override
public void onBackPressed() {

    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 0) {
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } else {
        super.onBackPressed();
    }
}

功能就像DrawerFragmentActivity(默认配置文件页面) - &gt; Product-&gt; Transactions。在交易屏幕上也可以看到抽屉图标,用户可以在使用抽屉的交易屏幕上再次点击我的卡片屏幕。

当用户点击产品时,它将再次打开交易页面,它工作正常。现在发生了什么,当我们点击返回事务时,它将出现在产品页面上,但是当我再次点击产品列表屏幕(Frame)与ProductsList和Transactions屏幕重叠时。

如果我感到困惑,我很抱歉,请问您是否理解。我可以解释。

感谢。

3 个答案:

答案 0 :(得分:3)

片段后退工作代码

public class ChiefFragment extends Fragment {
 View view;

// public OnBackPressedListener onBackPressedListener;

@Override
 public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle args) {

view = inflater.inflate(R.layout.activity_chief, container, false);
getActivity().getActionBar().hide();
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        Log.i(getTag(), "keyCode: " + keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            getActivity().getActionBar().show();
            Log.i(getTag(), "onKey Back listener is working!!!");
            getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            // String cameback="CameBack";
            Intent i = new Intent(getActivity(), home.class);
            // i.putExtra("Comingback", cameback);
            startActivity(i);
            return true;
        } else {
            return false;
        }
    }
});
return view;
}
}

答案 1 :(得分:0)

使用以下代码进行片段中的后退。

public class DashBoard extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard, container, false);
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK) {
 getActivity().finish(); }
 return true;
 }
 return false;
 }
});
}

此代码可帮助您实现backpreesed in片段。

答案 2 :(得分:-1)

我建议你实现一个管理backstack的界面。 Here是一篇很好的博文,可以帮助您理解这个过程