我在瀏覽器上瀏覽了很多東西,並且爲我的項目製作了一組最好的事務,但在處理壓抑時遇到了問題。在每個片段上處理壓縮
這是我baseactivity類:
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那裏我加入片段和處理backpressed事件:
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偵聽器。爲什麼?
維護currentfragment和onBackPress檢查(currentfragment}這種片段),做任何你想在OnBackPress – AbhayBohra