I had a problem when I change orientation of fragment. I had only one activity with some fragments. Activity run fragment1 => fragment1 run fragment 2 So when I change orientation of fragment 2, I found the activty. what I did :
I add this line to all the fragments :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// retain this fragment
setRetainInstance(true);
}
and in activity onCreate I did this :
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fragmentLayout);
findViewById(R.id.fragmentLayout).setVisibility(View.VISIBLE);
Map<String, Object> map = (HashMap<String, Object>) getLastCustomNonConfigurationInstance();
// Check if we are restoring the activity from an orientation change
// for example
if (map == null || map.isEmpty()) {
extras = getIntent().getExtras();
if (extras != null && extras.getInt("CategoryId") >0 ) {
fragment2 itemsFragment = new fragment2();
fragment2.setArguments(extras);
SplashActivity.this
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentLayout, itemsFragment).addToBackStack("fragment2").commit();
}
}
so my question, how I can change orientation of fragment 2 and stay in fragment2 ?