recyclerview is empty when back pressed from fragment

Viewed 4562

On Fragment1 i have onClick method, when i click the button, i got the recycleview list of items. When i click the item in recycleview(use getPlaceFromItem method), i go to the Fragment2. if device is phone then put the Fragment2 in container1, but if device is tablet(landscape) then i put Fragment2 into container2 next to Fragment1 in container1.

Now, when i press back button when device is phone, i get empty recycleview.

In addition i check orientation of the screen and when the device is phone and i am in Fragment2(i.e Fragment2 in container1), then when the screen will be landscape i will put into container1 Fragment1, and into container2 at Fragment2. My problem is how can i cut Fragment2 from container1 and put it into container2.

Main Activity :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {

                View v = findViewById(R.id.container2);
                v.setVisibility(View.GONE);

                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container1, new FragmentA())
                        .commit();

        }

 @Override
public void getPlaceFromItem(Place place) {

    Bundle bundle = new Bundle();
    bundle.putDouble("lat", place.getLat());
    bundle.putDouble("lng", place.getLng());
    Fragment2 fragment2 = new Fragment2();
    fragment2.setArguments(bundle);

    if(getResources().getBoolean(R.bool.isTab)) {

        View v = findViewById(R.id.container2);
        v.setVisibility(View.VISIBLE);

        getSupportFragmentManager().beginTransaction()
       .replace(R.id.container2,fragment2)
       .addToBackStack(null)
       .commit();
    }
    else {


        getSupportFragmentManager().beginTransaction()
        .replace(R.id.container1, fragment2)
        .addToBackStack(null)
        .commit();
    }

}


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



     @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);

            // Checks the orientation of the screen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {


                    View v = findViewById(R.id.container2);
                    v.setVisibility(View.VISIBLE);

                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container1, new Fragment1())
                             // The next row is a problematic!!!
                            .replace(R.id.container2, new Fragment2())
                            .commit();      

       } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
 }      
    }
4 Answers
Related