Hi stackoverflow friends. I have an player music and I call in oncreate playSong(id)(id is index of an array which has a path music in that index of array.But there is this problem that when rotates Screen then songs over and over plays from first index in array. I guess becuase oncreate method is called after each time event rotation. Also I want my app prevent from rotation becuse may be this event have bad effect in others activity.too, How I can fix these type of issues?
Asked
Active
Viewed 3,736 times
4 Answers
4
You can use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
to lock the orientation. Because as you mentioned, the onCreate
is called everytime the screen rotates.
But you have to call it before the setContentView(R.layout.main)
Could look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
}

Fraggles
- 463
- 4
- 20
-
but I have many activitis and I fair of happening this event at them. – sarah Jan 15 '14 at 14:58
-
You want it only in one single Activity? – Fraggles Jan 15 '14 at 15:03
-
I dont understand why oncreate() recall again? I want understand it. and I am not sure like these events havent bad effect in my activitis. Such as activitis which has datasource and list or other things when there are in oncreate(). – sarah Jan 15 '14 at 15:07
-
1because it has to adjust the UI to a "new" size. For example you start your app in Portrait mode, so you have a 1080*1920 resolution. If you now switch to landscape you have a 1920*1080 resolution. So the layout has to be adjusted to work proper. Thats why it redraws your UI, and your UI is set in the onCreate, so it has to call the onCreate :) – Fraggles Jan 15 '14 at 15:14
-
Then I must add this line:android:configChanges="orientation|screenSize|keyboardHidden" in all my activitis? – sarah Jan 15 '14 at 15:32
-
in those activities where you need it – Fraggles Jan 16 '14 at 06:38
3
you can do like this android:configChanges="orientation|keyboardHidden|screenSize"
hope it will help you
add it to manifest as follows as an example
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Jitesh Upadhyay
- 5,244
- 2
- 25
- 43
-
-
And why you add keyboardHidden and screenSize? these events makes force my app for recall oncreate() may be? yeah? – sarah Jan 15 '14 at 15:02
-
1yeah visit http://developer.android.com/training/multiple-apks/screensize.html and http://developer.android.com/reference/android/content/res/Configuration.htmlfor detail – Jitesh Upadhyay Jan 15 '14 at 15:05
-
1i use this android:configChanges="orientation|screenSize|keyboardHidden"> for screen size changing and when keyboard hiden. but for orientaion cant help me. – sarah Jan 16 '14 at 07:10
1
You could add android:configChanges="orientation" to Activity tag on AndroidManifest.xml. Try it.

Ravind Maurya
- 977
- 15
- 24
0
You could add android:configChanges="orientation|screenSize" to Activity tag on AndroidManifest.xml. It will helpful.

Sujatha Girijala
- 1,141
- 8
- 20