سؤال

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?

هل كانت مفيدة؟

المحلول

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); 
}

نصائح أخرى

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>

You could add android:configChanges="orientation" to Activity tag on AndroidManifest.xml. Try it.

You could add android:configChanges="orientation|screenSize" to Activity tag on AndroidManifest.xml. It will helpful.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top