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.
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"/>
I have a switch in my Android app settings to turn screen rotation on and off - just the way the user likes it to be.
But it does not disable in my app. I tried
_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
but rotation is still on. I know I can do this in the manifest file, but I want it to be configurable and not static.
You need to set setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); inside onCreate() method in your activity.
Or since that method is not always called you could put it in the onResume() method of your activity.
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.
Hopefully this isn't a dumb question, but when I initially click on my app, a kind of splash screen first appears for a few seconds with just a title bar indicating the name of the app and nothing else - then the app loads as expected. Is there any way to remove this screen?
The system looks into your AndroidManifest.xml to find out what is the theme of the main Activity. If you don't specify it manually, it assumes the application theme, if you don't specify application theme then the default theme is used. The system then creates the "splash screen" solely acoording to the theme.
So if you want to remove it, create a theme that has a transparent background and no ActionBar. And in onCreate() you probably want to change the theme again (or just change the background and show the ActionBar).
But it's better to have some visible "splash screen". Otherwise if you click on the icon, you won't see nothing for 1 - 2 seconds, which is quite weird, almost no app does this.
Initializing more resources on
onCreate()
may take some time. Try not to overload onCreate() method of Activity.
Try not to deep down more than 30 levels inside the xml layout file. which can also take up some time to initialize .
setContentView(R.layout.main)
I have another question!
I have a configure Activity in my app. On it, I have a ListView. Now adding & removing items to & from it works, but when I rotate the screen to landscape or back, all added & non-saved items in the ListView get removed... I wonder why this is... Why is this?
When screen orientation changes, by default, the activity will be destroyed and create again. That means, the onCreate will be called again once the orientation changed. There are basically two way to solve your problem:
Save your activity state before destory and load it back when create
Set the Activity in AndroidManifest that not to handle screen orientation change.
E.g.
<activity name="..."
android:configChanges="orientation" .../>
When you rotate the handset, the onStart method of you Activity is invoked. Check maybe you do some sort of initialization there.
I've solved it partially. Now it is able to hold on to the changes when changing to landscape view, but not the other way back to the normal view. I did it through the use of protected void onSaveInstanceState(Bundle saveState). But as I said, this doesn't work when it changes back from Landscape... Anybody got any ideas about this?