处理每个片段的背压

时间:2017-06-28 04:41:34

标签: android android-fragments onbackpressed

我在stackoverflow上浏览了很多内容,并为我的项目创建了一组最佳的事务,但在处理反向操作时遇到了问题。

这是我的基础活动课程:

public class BaseAppCompatActivity extends LifecycleActivity {
    protected IBackPressedListener onBackPressedListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    protected FragmentTransaction getFragmentTransaction() {
        return getSupportFragmentManager().beginTransaction();
    }

    protected void addFragment(int fragmentContainer, Fragment fragment, String fragName, boolean addToBackStack) {
        FragmentTransaction ft = getFragmentTransaction();
        ft.add(fragmentContainer, fragment);
        if (addToBackStack)
            ft.addToBackStack(fragName);
        // Complete the changes added above
        ft.commitAllowingStateLoss();
    }

    public void setOnBackPressedListener(IBackPressedListener onBackPressedListener) {
        this.onBackPressedListener = onBackPressedListener;
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onBackPressedListener = null;
    }
}

这是我的活动clas,我在这里添加片段并处理反向事件:

public class RechargeFragmentActivity extends BaseAppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_fragment);

        /*Replace fragment*/
        addFragment(R.id.fragmentFrame, new SelectRechargeOperatorFragment(), AppFragments.FRAG_SELECT_RECHARGE_OPERATORS, true);
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1 && onBackPressedListener != null) {
            onBackPressedListener.onBackPressed();
        } else
            finish();
    }

}

现在我在片段中实现了Onbackpressed侦听器,并且它可以正常地进行一次反向操作,之后它会选择仅弹出的最后一个片段的实例。

public class SelectRechargeOperatorFragment extends BaseFragment {
    @Inject
    public SelectRechargeOperatorLiveDataModel operatorLiveDataModel;
    @BindView(R.id.rv_select_operator)
    RecyclerView rvSelectOperators;
    @BindView(R.id.proceedLayout)
    RelativeLayout btnProceed;
    private RechargeOperatorsAdapter adapter;


    public SelectRechargeOperatorFragment() {
        //empty constructor required
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((BaseAppCompatActivity) fragmentContext).setOnBackPressedListener(this);
    }

    @Override
    protected void onInject(GraphComponent applicationComponent) {
        super.onInject(applicationComponent);
        applicationComponent.inject(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View convertView = inflater.inflate(R.layout.fragment_select_recharge_operator, container, false);
        ButterKnife.bind(this, convertView);

        /*fetch-views*/
        setUpOperatorRecyclerView();
        proceedToRecharge();

        return convertView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void proceedToRecharge() {
        btnProceed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addFragment(R.id.fragmentFrame, new RechargeAirtimeFargment(), AppFragments.FRAG_RECHARGE_AIRTIME_PLANS, true);
            }
        });
    }
 @Override
    public void onBackPressed() {
        super.onBackPressed();
        getActivity().getSupportFragmentManager().popBackStack();
    }


}

当我只添加一个片段时,这很有效。当我添加多个片段时,它只适用于第一次弹出片段之后,它会返回旧片段onBackpressed侦听器。为什么?

1 个答案:

答案 0 :(得分:2)

问题是,每次添加片段时,新对象都会覆盖您活动中的onBackPressedListener。因此,在弹出最后一个片段后,逻辑失败。请尝试以下方法。

  1. 从您的活动中删除setOnBackPressedListeneronBackPressedListener
  2. 在片段中实施IBackPressedListener。可能是您可以使用basefragment来避免代码重复。根据您的需要选择。
  3. 将您活动中的onBackPressed更改为

     @Override
     public void onBackPressed() {
            if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragmentFrame);
               if(null != currentFragment && currentFragment instance of IBackPressedListener) {
                   ((IBackPressedListener)currentFragment).onBackPressed();
               }
            } else
                finish();
        }