onBack压缩活动和片段

时间:2016-05-04 05:22:35

标签: android android-fragments android-activity onbackpressed

带有碎片的导航抽屉。我在主要活动上放了一个片段。但如果我再次按下相同的片段再来。我该怎么处理? 第二,如何在片段中实现onBackpressed? 有人可以帮帮我吗?

在onCreate主要活动中我有

    FragmentTransaction tx = getFragmentManager().beginTransaction();
    tx.replace(R.id.mainFrame, new PlaceOrderFragment());
    tx.commit();

在我的背压下我有

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

4 个答案:

答案 0 :(得分:0)

        FragmentTransaction tx = getFragmentManager().beginTransaction();
            tx.replace(R.id.mainFrame, new PlaceOrderFragment()).addToBackStack("your tag");
            tx.commit();


        Override onBackPress() and try following code.

        @Override
        public void onBackPressed() {

            int count = getFragmentManager().getBackStackEntryCount();

            if (count == 0) {
                super.onBackPressed();
                //your other utility code if needed
            } else {
                getFragmentManager().popBackStack();
            }

    }

Hope this will work for you.

答案 1 :(得分:0)

您可以使用Interface执行此操作。该接口将在每个片段中实现。这是一个简单的例子:

public interface OnBackPressedListener {
    void onBackPressed();
}

在这里,如果您在片段中实现此接口,那么您将找到此方法:

@Override
    public void onBackPressed() {
        getActivity().finish(); // Or Do your specific task

    }

现在你的Activity包含那个应该使用onBackPressed()回调的片段。在该方法中写下波纹管代码

@Override
    public void onBackPressed() {

        if (YOUR_DRAWER.isDrawerOpen(GravityCompat.START)) {
            YOUR_DRAWER.closeDrawer(Gravity.LEFT); //CLOSE Nav Drawer!
            return;
        }

        Fragment fragment=getYourVisibleFragment();
        if(fragment!=null){
            ((OnBackPressedListener)fragment).onBackPressed();
        }else{
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

如果你想获得Visible片段,那么就使用这个方法

public <F extends Fragment> F getVisibleFragment() {
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                if (fragment != null && fragment.isVisible()) {
                    return (F) fragment;
                }
            }
        }
        return null;
    }

那就是:)

答案 2 :(得分:0)

首先,您需要将fragmnet添加到后台堆栈中,以便稍后在onBackPressed()

中检索它

用此

替换您的fragmnet事务
FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.replace(R.id.mainFrame, new PlaceOrderFragment());
// HERE WE WILL ADD FRAGMENT TO BACKSTACK SO WE COULD GET BACK TO IT
tx.addToBackStack(null);
tx.commit();

将此onBackPressed替换为此

@Override
public void onBackPressed() {
    // CHECKS HOW MANY FRAGMENTS ARE IN THE BACKSTACK
    int count = getFragmentManager().getBackStackEntryCount();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        if (count == 0) {
                // IF NO FRAGMENT IN THE BACKSTACK CALL
                super.onBackPressed();
            } else {
                // GET THE LAST OPENED FRAGMENT
                getFragmentManager().popBackStack();
            }
    }
}

答案 3 :(得分:-2)

将onCreate中的ypu片段替换为片段id,然后在同一个活动上实现onBackpressed并找到带有id的片段并检查它是否可见或为null,然后根据它执行操作。 希望你能得到这个。