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
Asked
Active
Viewed 72 times
0
-
do you want that other fragment should be in portrait only ? – Amit Vaghela Dec 29 '15 at 05:08
-
no i think rotation would be good for my app. how would i do that, turn off rotatin – vin shaba Dec 29 '15 at 05:14
-
okay it got. it think your app do not retain to same state when you rotate it changes fragment on rotate ? – Amit Vaghela Dec 29 '15 at 05:15
-
If you are adding multiple fragments to your activity then when you rotate your phone , activity gets re created and all your fragments get displayed again . You have to store the current fragment at the time of rotation and display it back when rotated – Vivek Mishra Dec 29 '15 at 05:22
-
check my answer below it will solve your issue. – Amit Vaghela Dec 29 '15 at 05:24
-
yeah @mishra thats the problem how do i do that – vin shaba Dec 29 '15 at 05:25
-
please accept answer if it is helpfull. @vinstar – Amit Vaghela Dec 29 '15 at 13:25
2 Answers
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
-