Popbackstack在OnBackPressed中完成活动

时间:2015-05-14 06:49:17

标签: android android-fragments

我有一个带有两个片段的活动(List& Details),在onCreate()中,如果savedInstance为null,我就会启动List Fragment

 public static final int CLIENT_LIST_FRAGMENT = 1;
 public static final int CLIENT_DETAILS_FRAGMENT = 2;
 private int currentFragment = 0;

if (savedInstanceState == null) {
            //On first run, start the list fragment
            if (currentFragment == 0) {
                ClientListFragment listFrag = new ClientListFragment();
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container_client, listFrag, Constants.CLIENT_LIST_FRAGMENT_TAG)
                        .commit();
                currentFragment = CLIENT_LIST_FRAGMENT;
            } else {
                switch (currentFragment) {
                    case CLIENT_LIST_FRAGMENT:
                        addListFragment();
                        break;
                    case CLIENT_DETAILS_FRAGMENT:
                        addDetailsFragment(0);
                        break;
                }
            }
        }

这是根据需要添加DetailsFragment和ListFragment的方法

 private void addListFragment(){
        ClientDetailsFragment listFrag = new ClientDetailsFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container_client, listFrag, Constants.CLIENT_LIST_FRAGMENT_TAG)
                .addToBackStack("details")
                .commit();
        currentFragment = CLIENT_LIST_FRAGMENT;
    }

    private void addDetailsFragment(long companyId){
        ClientDetailsFragment detailsFrag = ClientDetailsFragment.newInstance(companyId);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container_client, detailsFrag, Constants.CLIENT_DETAILS_TAG)
                .addToBackStack(null)
                .commit();
        currentFragment = CLIENT_DETAILS_FRAGMENT;

    }

除了当我单击Details Fragment中的后退按钮而不是将我带回List Fragment,它实际上完成了Activity时,它按预期工作。它会在一瞬间返回到List Fragment,然后完成主机Activity并返回到调用Activity。我怎么能阻止这种行为,经过很多关于onBackpress的相关问题在这里是我的overBackPressed

@Override
    public void onBackPressed() {
        if (currentFragment == CLIENT_DETAILS_FRAGMENT){
            getSupportFragmentManager().popBackStackImmediate();
        }else {
            super.onBackPressed();
        }
    }

谢谢

4 个答案:

答案 0 :(得分:1)

我经常检查当前片段实例是否属于调用 onBackPressed()的片段。此代码未经测试;它只是为了让您了解如何继续。我希望你不要在Activity上调用 this.finish()

@Override
public void onBackPressed() {
    Fragment fragment = getSuportFragmentManager().findFragmentByTag(Constants.CLIENT_DETAILS_TAG);
    if (fragment instanceof ClientDetailsFragment) {
        getSupportFragmentManager().popBackStackImmediate();
    }
    else {
        super.onBackPressed();
    }
}

如果有帮助,请告诉我。

答案 1 :(得分:1)

活动onBackPressed()应为:

对于android.support.v4.app.Fragment

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 1)
        getSupportFragmentManager().popBackStack();
    else
        finish();
}

对于android.app.Fragment

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 1)
        getFragmentManager().popBackStack();
    else
        finish();
}

答案 2 :(得分:0)

好主啊,我正在追逐错误的影子两天。我正在倾听,处理和压倒错误的事件。

这个SO问题救了我Why is onBackPressed() not being called?

当用户点击硬件后退按钮(或导航栏中的“向上”按钮)而不是操作栏中的按钮时,gist是“onBackPressed()被调用。对于这个你需要覆盖onOptionsItemSelected()方法。例如:“

我正在抓住这个事件片段并调用getActivity.onBackpressed。当我停止这一点时,我很明显没有调用onBackPressed。这是我的解决方案,可能不是最优的但是有效

if (item.getItemId() == android.R.id.home){
            if (currentFragment == CLIENT_DETAILS_FRAGMENT){
                getSupportFragmentManager().popBackStackImmediate();
                currentFragment  = CLIENT_LIST_FRAGMENT;
                return true;
            }
        }

答案 3 :(得分:0)

 if (currentFragment == 0) {
            ClientListFragment listFrag = new ClientListFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container_client, listFrag, Constants.CLIENT_LIST_FRAGMENT_TAG)
                    .commit();
            currentFragment = CLIENT_LIST_FRAGMENT;
        }

更改此内容时添加到addToBackStack()

和, 改变onBackPressed()喜欢,

@Override
public void onBackPressed() {
 if (getFragmentManager().getBackStackEntryCount() == 1) {
     MainActivity.this.finish();
 }
 else {
    getFragmentManager().popBackStack();
  }
}

希望,这会奏效。