片段和OnBackPressed

时间:2016-11-11 13:30:06

标签: android android-fragments onbackpressed

我使用一个Activity作为fragments的容器。 有一个问题:

如果您打开ProfileEditFragment并按后退按钮,则成功调用onBackPressed()中的方法MainActivity然后接下来发生:

1)打开MapFragments过去状态

2)打开我们按下后退按钮的ProfileEditFragment(原因不明)

我试图使用getSupportFragmentManager().popBackStack()但它没有帮助(对回复没有反应)。 我在调用onBackPressed()之前检查了堆栈的内容,并且它不是空的。

MainActivity:

 @Override
    public void showMapFragment() {
//        getSupportFragmentManager().popBackStack(null,getSupportFragmentManager().POP_BACK_STACK_INCLUSIVE);
        //mainPresenter.clearActiveLoginFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.ltContainer, MapFragment.newInstance())
                .addToBackStack(null)
                .commit();
    }

    @Override
    public void showProfileEditFragment() {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.ltContainer, ProfileEditFragment.newInstance())
                .addToBackStack(null)
                .commit()
        ;
    }

    @Override
    public void onMainBackStack() {

        int count = getSupportFragmentManager().getBackStackEntryCount();
        Log.d("COunt in backstack", count + "");
//        getSupportFragmentManager().popBackStack();
//        Log.d("COunt in backstack",getSupportFragmentManager().popBackStackImmediate()
//        +"");
//        getSupportFragmentManager().popBackStack();
//        getSupportFragmentManager().popBackStackImmediate();
        super.onBackPressed();
    }

ProfileEditFragment

private static final int LAYOUT = R.layout.fragment_profile_edit;


    //@formatter:off
@InjectPresenter ProfileEditPresenter mProfileEditPresenter;
//@formatter:on
public static ProfileEditFragment newInstance() {
    ProfileEditFragment fragment = new ProfileEditFragment();

    Bundle args = new Bundle();
    fragment.setArguments(args);

    return fragment;
}

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         final Bundle savedInstanceState) {
    View view=inflater.inflate(LAYOUT, container, false);
    ButterKnife.bind(this,view);
    return view;
}

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

}

MapFragment

//@formatter:off
    @InjectPresenter MapPresenter mapPresenter;
    @BindView(R.id.map) ru.yandex.yandexmapkit.MapView mapView;
    @BindView(R.id.fbMenu) FloatingActionButton fbMenu;
    //@formatter:on

    private MapController mapController; //// FIXME: 11.11.2016

    public static MapFragment newInstance() {
        MapFragment fragment = new MapFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             final Bundle savedInstanceState) {
        View view = inflater.inflate(LAYOUT, container, false);
        ButterKnife.bind(this, view);
        setupMap();
        showMapButtons();
        mapPresenter.checkPermission();
        setupListeners();
        return view;
    }

    private void setupListeners() {
        fbMenu.setOnClickListener(v->mapPresenter.onClickMenu());
    }

    private void setupMap() {
        mapController = mapView.getMapController();
        mapController.getMapRotator().a(true);
    }

    @Override
    public void showMapButtons() {
        mapView.showZoomButtons(true);
        mapView.showScaleView(false);
        mapView.showJamsButton(false);
    }

    @Override
    public void showProfileEditFragment() {
        ((MainActivity) getActivity()).showProfileEditFragment();
    }

0 个答案:

没有答案