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