Changing orientation of the device refreshes the fragments activities - java

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

Related

activity orientation change bug

I'm experiencing a weird bug that I haven't seen in an Android application before. When navigating from an activity locked in portrait mode to an activity locked in landscape mode and back, the activity that's supposed to be in portrait appears in portrait mode, goes to landscape and then back to portrait.
This happens while the device is flat on the table with no actual orientation changes. I'm using two separate activities, and that's the only code that I have in the project.
Here is a video of the bug, and here's a link to the exact project that reproduces this bug. It happens on more than one device so it's not isolated to my device.
Questions:
Do you know what could be causing this?
Is there anything that you can recommend for fixing this?
Things I've tried:
Setting the orientation programatically
Googling and not finding anything
Update 1
More things I've tried:
Setting the portrait activity to "nosensor" and the landscape one to "landscape"
Setting the portrait activity to "nosensor" and programatically setting the landscape activity in onCreate
Update 2
I've been working with the project linked above and just did some overrides to log out everything that's happening. I found that when the onConfigurationChanged is called it does the little shimmy between the landscape and the portrait orientations. The output for a back navigation that doesn't do the shimmy is:
D/class com.mdk_studio.orientationbugtests.MainActivity: onStateNotSaved
D/class com.mdk_studio.orientationbugtests.MainActivity: onRestart
D/class com.mdk_studio.orientationbugtests.MainActivity: onWindowFocusChanged
The output for the shimmy bug is:
D/class com.mdk_studio.orientationbugtests.MainActivity: onStateNotSaved
D/class com.mdk_studio.orientationbugtests.MainActivity: onRestart
D/class com.mdk_studio.orientationbugtests.MainActivity: onWindowFocusChanged
D/class com.mdk_studio.orientationbugtests.MainActivity: onConfigurationChanged
D/class com.mdk_studio.orientationbugtests.MainActivity: onConfigurationChanged
I guess the question now becomes, how do I make sure the orientation change does not get called?
After testing out all the different combinations of manifest level orientation setting and programmatic orientation setting. I have figured out that for the orientation to not be wrong in the onConfigurationChanged function you have to set the orientation before the navigation is actually started.
The combination that worked for me and took the behaviour away completely was to set the activity in the manifest that I want in portrait to "nosensor". Then set the activity that I want in landscape to landscape using
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
and set the orientation back to portrait in the onBackPressed before the navigation occures. That part is important, once the navigation has been triggered and then you set the orientation, in some cases when the newConfiguration comes through in the onConfigurationChanged call it has the wrong orientation, and then it somehow issues another call to it to correct it after it's been updated on the first call.
tl;dr
Set the orientation for the activity you want in portrait to "nosensor". Programatically set the orientation to landscape in onCreate. Set the orientation back to portrait in onBackPressed, before the super call.

auto rotate stops music when app rotates

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.

On change screen orientation, new activity created

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.

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