Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recyclerview is empty when back pressed from fragment

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){
 }      
    }
like image 941
Ira Avatar asked Aug 01 '17 07:08

Ira


1 Answers

Remember when a fragment is popped out from back stack it comes back with no view. So you have to attach the view again.

I recommend for you to keep track of the main View in Fragment in order to avoid views from recreating. This way once you come back to the fragment with the RecyclerView it will be there. The code would look like this.

public class BlankFragment2 extends Fragment {

public View myRoot;

public BlankFragment2() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    if (myRoot == null) {
        myRoot = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
    }
    return myRoot;
}

}

like image 174
Cesar Garcia Avatar answered Oct 21 '22 02:10

Cesar Garcia