I'm trying to show two Fragments
in a FrameLayout
container. I only show one at a time and hide another one based on my application logic. I believe every time I change my device orientation, they recreate! I can see duplicates of fragments overlapping each other! How to fix it? Thanks in advance.
public class MainActivity extends AppCompatActivity {
final Fragment oneFragment = oneFragment.newInstance();
final Fragment twoFragment = twoFragment.newInstance();
Fragment active = oneFragment;
final FragmentManager fm = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some other code
fm.beginTransaction().add(R.id.container, oneFragment, "1").commit();
fm.beginTransaction().add(R.id.container, twoFragment, "2").commit();
// onclick listener
if(condition) {
fm.beginTransaction().hide(active)
.show(oneFragment).commit();
} else {
fm.beginTransaction().hide(active)
.show(twoFragment).commit();
}
// some other code