0

i have an app with various fragments and the problem is when the phone rotates, the app displays other fragments from the begining. it does not close the current fragments but looks like layers on top of each other. any help i'd be grateful. thanks

vin shaba
  • 167
  • 3
  • 12

2 Answers2

0

Try this to add in your AndroidManifest

     <Activity 
        ....
        ....
        android:configChanges="orientation|screenSize">
Milos Lulic
  • 627
  • 5
  • 22
0

You need to check for a savedInstanceState, and if it exists, don't re-create your fragments. just check if is null or not

as shown below

    @Override        
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            if (savedInstanceState == null) {
                 // Do your oncreate because there is no bundle   
            }else{
        // Do that needs to be done even if there is a saved instance, or do nothing
    }
    }

check this for more detail.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • okay so i put ALL my activity code in the if, then i do nothing in the else, is that it? – vin shaba Dec 29 '15 at 05:37
  • no, you need to maintain state of old fragment with super.onCreate(savedInstanceState);. So maybe set tag and find the fragment if it exist, or pass null bundle to super. find out more http://developer.android.com/guide/components/fragments.html – Amit Vaghela Dec 29 '15 at 05:40
  • was this helpfull ? @vinstar – Amit Vaghela Dec 29 '15 at 05:58