8

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){
 }      
    }
5
  • is resyclerview is on same container fragment? Aug 1, 2017 at 7:21
  • Yes, on the same container fragment.
    – Ira
    Aug 1, 2017 at 7:33
  • Can you please add the code of your Fragment onCreateView and onResume methods?
    – Eselfar
    Aug 1, 2017 at 9:32
  • Okay, I will add code later. Thank you
    – Ira
    Aug 1, 2017 at 9:46
  • I added more code, please see it.
    – Ira
    Aug 2, 2017 at 5:07

4 Answers 4

8

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;
}

}

5
  • i will check later and tell you what happened. Thank you
    – Ira
    Aug 1, 2017 at 8:08
  • Before a minute i put more code, can you see it please.
    – Ira
    Aug 2, 2017 at 5:06
  • Sorry this is a completely new question and code i think you should calificate first the last one , and then add a new question. Aug 2, 2017 at 6:23
  • I will try your idea today and back with result.
    – Ira
    Aug 2, 2017 at 6:40
  • I checked the code, i have the same rows as you suggested, but it doesn't help.
    – Ira
    Aug 2, 2017 at 18:55
0

.addToBackStack(null) remove it from code

i hope it will work for you

3
  • i suggest that fill recyclerview in onResume() or onPause() again Aug 1, 2017 at 7:37
  • what do you mean? can you let me an example please?
    – Ira
    Aug 1, 2017 at 7:44
  • I added more code and explanation. Can you see it please.
    – Ira
    Aug 2, 2017 at 5:09
0

Add .addToBackStack(fragment.getClass().getCanonicalName()) or .addToBackStack("anyString") to save fragment instance. Also use add method, not replace when you add new fragments.

3
  • Did you use add? replace will remove your last fragment add new one. add just will add new one. Aug 1, 2017 at 7:56
  • I will add more code later. In used replace, because there are yet other fragment in the container1.
    – Ira
    Aug 1, 2017 at 8:01
  • I added more code and explanation. Can you see it please.
    – Ira
    Aug 2, 2017 at 5:08
0

when you are adding your first fragment you should not use addToBackStack(null).

for first fragment write like this

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

for other fragments you should use

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

hope it helps!

2
  • Before a minute i added more code and explanation. Can you see it please.
    – Ira
    Aug 2, 2017 at 5:08
  • It does't help.
    – Ira
    Aug 3, 2017 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.