On change screen orientation, new activity created - java

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.

Related

Changing orientation of the device refreshes the fragments activities

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"/>

Android - FlipView reset by itself

I'm pretty new in Android development, so i'm not sure what is happening.
I have made an app with a fixed part outside and a part inside that switch a FlipView during onCreate event and onNavigationItemSelected. All work fine, exept when i flip my phone to landscape and back, the flipview automaticaly return on it's original page.
Your Activity recreated, when you turn device. Read THIS to solve this problem. You need to implement onSaveInstanceState function.

Android screen on during entire application

I'm writing an Android app that connects to a computer and acts as a mouse. I want the screen to remain awake only while the app is being used. I know I can use getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
to keep the screen on during an activity, but is there a way to keep the screen on for all activities or would I have to use that line of code in every onCreate method for each activity?
You would have to use that line within each activity.
Or you could use Fragments and set it once in the only activity and it will be applied to each fragment from then on.

Why if rotate my android phone my application gui settings has dropped?

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.

Why is it that when I swap to landscape, my changes to the User Interface are undone?

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?

Categories