1

I have application with activity and nested Fragment inside of it.

Adding nested Fragment was working fine when i was locked the activity orientation to portrait.
Then I wanted to handle some thing onConfiguration changed so I removed screen orientation from the manifest and I locked the activity programmatically and I overrided onConfiguration changed in my activity.

But in this case the I try to set a Fragment on top of activity.

Its onCreateView is called but it doesn't appear on the screen.

I have checked the solution described here but it didn't worked. link

that's my code for replacing fragments

    protected void setFragment(Fragment fragment) {

    FragmentManager fragmentManager = getSupportFragmentManager();

    FragmentTransaction fragmentTransaction;
    fragmentTransaction =
            fragmentManager.beginTransaction();

    fragmentTransaction.isAddToBackStackAllowed();

    fragmentTransaction.replace(R.id.container, fragment);
    //  fragmentTransaction.addToBackStack("frag1");

    fragmentTransaction.commit();
}

protected void addFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.defff, R.anim.defff, R.anim.right_out);
    fragmentTransaction.isAddToBackStackAllowed();
    fragmentTransaction.add(R.id.container, fragment);
    fragmentTransaction.addToBackStack("frag2");
    fragmentTransaction.commit();
}

and the onConfiguration change

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

    RTA.setLocale(getApplicationContext(), RTA.getSharePrefrenceLocale(getApplicationContext()));
    //System.out.println("device rotated");getWindowManager().getDefaultDisplay().getRotation();


    if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_270) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Intent Madinti = new Intent(this, Madinati.class);
        Madinti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Madinti.putExtra("side", 1);


        getApplicationContext().startActivity(Madinti);
    } else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_90) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Intent Madinti = new Intent(this, Madinati.class);
        Madinti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Madinti.putExtra("side", 0);


        getApplicationContext().startActivity(Madinti);
    } else {

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

    }
}

and the activity in the manifest

  <activity
        android:name=".Parking.click_add"
        android:configChanges="orientation|keyboardHidden|screenSize|locale"
        android:label="@string/title_activity_click_add"
         />

and here is onCreate and onAttach of the secondly added fragment

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    if (mRetainedChildFragmentManager != null) {
        //restore the last retained child fragment manager to the new
        //created fragment
        try {
            Field childFMField = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFMField.setAccessible(true);
            childFMField.set(this, mRetainedChildFragmentManager);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    } else {
        mRetainedChildFragmentManager = getChildFragmentManager();
    }
}  

any help Thanks

Antwan
  • 3,837
  • 9
  • 41
  • 62
  • Did you get any exception on stacktrace? – Anggrayudi H Oct 17 '15 at 12:17
  • Nope setfragment work but when i want to add another fragment its layout doesnt appear – Antwan Oct 17 '15 at 12:18
  • Without code, not sure whether this is similar to http://stackoverflow.com/questions/30671291/why-does-rotation-cause-android-fragment-replacement-to-fail – mattm Oct 17 '15 at 12:51
  • Can you share your code? – Zsolt Mester Oct 17 '15 at 21:21
  • Hey thanks for you help guys – Antwan Oct 18 '15 at 07:46
  • @mattm in the question you suggest i think it's deferent problem i have the first fragment is is at the start but adding another fragments doesn't works even if i didn't rotated my device – Antwan Oct 18 '15 at 07:47
  • No i have a header which is not override by the fragment and i have one fragment is added in the activity oncreate and on some activity i have another fragment added ,and i want my fragment appear in landscape but i use orientation to open another activity in landscape more ,so you can say that my activity locked on portrait – Antwan Oct 18 '15 at 08:42

1 Answers1

0

call setRetainInstance(true); in OnActivityCreated Callback in the fragment.

srj
  • 90
  • 7
  • I tried this in onCreate i will check if it works Thanks – Antwan Oct 23 '15 at 13:14
  • Thanks man it works even if i changed all the code structure because i didn't get the answer but i tested it on old code and it works – Antwan Oct 23 '15 at 18:17