I added
android:screenOrientation="portrait"
To the activity in the manifest but the app still rotates if the device is turned. Cannot add all of code yet, on phone. But will add when i get back home.
You can set the orientation in two ways:
I. Programatically:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
II. In your AndroidManifest.xml
<activity android:name=".activity"
android:screenOrientation="portrait">
</activity>
It's hard to understand what you need from the few lines you wrote. It looks like you are creating a music player app and you are facing the problem that music stops playing when changing the orientation of the device. It also looks like your solution to this problem is to prevent screen orientation changes. If I got it right, you can prevent screen orientation by putting the following line in your Activity tag inside the manifest:
android:configChanges="orientation|screenSize"
The music stops playing because when screen orientation changes, the system destroys and recreates the Activity. If you cut away screenSize from the line above, the system will not destroy your Activity, thus the music will keep playing, but the user will still be able to rotate the screen. Moreover, if your application is actually a music player I would consider other ways of implementing it, such as background services.
Related
I realized a problem in my app, I use BottomNavigation with FragmentActivities, the problem was that when clicking one of the 5 FragmentActivities. 5 activities are [Home, Search, Post, Notifications & Profile] if I click any of them being in portrait state and change the device orientation to landscape the activities refreshes and start to [Home] screen always.
How do I fix this?
maybe you can try add configuration in your main_activity in manifest
<activity
android:name=".your.main_activity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
If i set rotation for the inner activity the whole screen rotates. I only want the inner activity to turn into landscape mode, not the outer activity.
This is a screenshot in which you can see 2 activities. One is the whole screen another is the camera that scans a QR code:
Add the below mentioned attribute in your Activity tag inside AndroidManifest.xml
android:screenOrientation="landscape"
Ex:
<activity android:name=".MainActivity" android:screenOrientation="landscape">
I'm relatively new to Android but have made quite a few apps over the past year so forgive me.
I know that when you are running an app on a device and change the screen orientation by turning the device the activity that is showing is completely recreated. I go to the youtube app (I am using Nexus 7 w/ Android 4.2.2) and play some video and then flip the screen to change orientation. The video keeps playing and everything is re-sized accordingly... How is this possible if the activity is completely recreated?
Thank you.
In your AndroidManifest.xml add, android:configChanges and this would avoid the activity being re-created on orientation change. I hope your landscape and portrait mode has the same layout.
<activity android:label="Your Activity Name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.yourpackage">
To add more to this, i would suggest you to look at the onPause() and onResume() methods and if you are playing with Fragments then have a watch on onSaveInstanceState and onRetainInstanceState rather than applying the xml changes as "Activity is destroyed by design."
http://developer.android.com/training/basics/activity-lifecycle/index.html
If you are worried about losing the value of local variables within your activity... You could use Singleton Pattern for some of those variables. This not a beautiful solution, but could work.
On my app, when you tilt the device in the mapView, it redraws all the markers. I want to not let this happen.
To avoid the reloading of the map on screen orientation change I've added in the manifest, in the activity tag this line:
android:configChanges="orientation"
I don't know if it's the best solution but it works
Try using;
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
For example, I have write GUI. I have a SeekBar, I change the progress but if I rotate android phone the progress is dropped as default. Why it's happend and how could I fix it?
This is because androids reloads the activity when the screen is rotated. You can disable the rotaion by adding android:screenOrientation="portrait" to the element in the manifest or landscape.
Or you will need to store the activity's state elsewhere and onload of the activity, set the values from your store.
Because the Activity objects are re-created whenever the orientation changes.
Store the progress somewhere else.