0

I'm using one Activity as container for fragments. There is a problem:

If you open the ProfileEditFragment and press on the back button, then successfully call a method onBackPressed() in MainActivity and then happens next:

1)Open MapFragments past state

2)Open ProfileEditFragment from which we have pressed the back button(for unknown reasons)

I'm tried to use getSupportFragmentManager().popBackStack()but it did not help (there was no reaction to the press back). I check the contents of the stack before the call onBackPressed() and it's not empty.

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();
    }
Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25
no news
  • 1,060
  • 3
  • 14
  • 24
  • [Check this answer](http://stackoverflow.com/a/8163610/4700782) – Pier Giorgio Misley Nov 11 '16 at 13:34
  • What order do you add those fragments in? Are you certain you're not adding them multiple times? Also, there's no need to manually pop back stack or override onBackPressed(). The system will handle it in your case. – Tomislav Turcic Nov 11 '16 at 13:43

0 Answers0