I am trying to set one particular fragment orientation as Landscape mode. I have added setRetainInstance(true); in onCreate method.
I have added getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); in onCreateView method.
It is working fine with 7.1 and lesser versions. But it is not working with 8.1.
I have also tried the following links.
Link1
Link2 and many other solutions.
But those solutions were applicable for activity. In my case, I am using a fragment. Could anyone help me to solve this issue?
I have the following code in my manifest.
<activity
android:name=".activity.HomeScreen.view.HomeActivity"
android:label="#string/title_activity_home"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
In the manifest look the Activity who contain the fragment and check if you have the following string:
android:configChanges="orientation|keyboardHidden|screenSize"
Remove it. It prevent to override landscape layout
Above code given by you in onCreateView method is working fine with fragment in both Android 8 oreo and android 9 pie.
If you haven't added onPause() in fragment please add
#Override
public void onPause() {
super.onPause();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
It's not required to set setRetainInstance(true); in onCreate() method for the above versions.
Check if it's a particular phone issue and please check if auto rotate is disabled in that phone.
Related
I have an activity which is supposed to be in landscape mode.
code for the Activity
public class SessionActivity extends BaseActivity {
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
Manifest.xml
<activity
android:name=".activities.SessionActivity"
android:screenOrientation="landscape">
</activity>
BaseActivty extends AppCompatActivity.
When I call start this activity through intent, this activity goes in loop. onDestroy is called after onCreate and then onDestroy again and the same thing repeats when device orientation is changed from portrait to landscape. This is only happening in very few devices not in all the devices.
Any suggestions on why it is happening or how can I solve it?
Thanks
There are few steps to make your app support orientations. You can not just expect your app to behave same way it is working in portrait mode.
Check this doc for more details.
Also please make sure to make landscape files separately for landscape support. For that you can check this doc
You can add below line in manifest file:
android:configChanges="orientation|screenSize"
I can control orientation in eclipse but I need now change orientation in the android studio.In eclipse I find android manifest file and solved the problem from the file.But in android studio, I only find manifest.xml file.
I also saw the link
So,how can I solve the problem?
You don't need to declare screen orientation in the Manifest.xml file, you can set the screen's orientation in the onCreate method in your Activity which is called when your program runs.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
You can use other orientations as well. This will keep the orientation the same throughout the program's lifecycle.
If however, you want it to return to another setting after OnDestroy is called, simply save the orientation setting using sharedPreferences (https://developer.android.com/reference/android/content/SharedPreferences.html) and then write an if else statement to change the value after the event.
To control orientation ,you have to override the attribute android:configChanges of activity in manifest.
like
`<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">`
for more detail go through this https://developer.android.com/guide/topics/resources/runtime-changes.html
I have an app that uses the below sdk in the manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
To prevent the activity from restarting on screen rotation I add to manifest :
android:configChanges="orientation|keyboardHidden|screenSize"
Do I need to override the onConfigurationChanged in activity itself also, as below:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}
or is this line in the manifest is enough:
android:configChanges="orientation|keyboardHidden|screenSize"
Any help will be appreciated.
To prevent the activity from restarting on screen rotation
That is usually not a very good idea.
I add to manifest : android:configChanges="orientation|keyboardHidden|screenSize"
That is usually even less of a good idea. All you do is create more work for yourself. You still have to have all of the standard configuration change logic, to handle all of the configuration changes that you do not have listed there, plus you may need to deal with these three cases separately.
Do I need to override the onConfigurationChanged
That depends upon whether your UI is different based upon configuration, for the configurations you have listed in android:configChanges (e.g., does portrait look different than landscape?). If the answer is "yes", then onConfigurationChanged() is where you will fix up that UI for the events listed in your android:configChanges roster. If the answer is "no", then you do not need to override this method.
The manifest piece below that you had is enough to prevent the rotation:
android:configChanges="orientation|keyboardHidden|screenSize"
Set
android:screenOrientation="portrait"
as suggested by Android - disable landscape mode?
I simply need nothing to change when the screen is rotated. My app displays a random image when it first loads and rotating the device should not select another random image.
How can I (simply) make this behavior stop?
There are generally three ways to do this:
As some of the answers suggested, you could distinguish the cases of your activity being created for the first time and being restored from savedInstanceState. This is done by overriding onSaveInstanceState and checking the parameter of onCreate.
You could lock the activity in one orientation by adding android:screenOrientation="portrait" (or "landscape") to <activity> in your manifest.
You could tell the system that you meant to handle screen changes for yourself by specifying android:configChanges="orientation|screenSize" in the <activity> tag. This way the activity will not be recreated, but will receive a callback instead (which you can ignore as it's not useful for you).
Personally I'd go with (3). Of course if locking the app to one of the orientations is fine with you, you can also go with (2).
Xion's answer was close, but #3 (android:configChanes="orientation") won't work unless the application has an API level of 12 or lower.
In API level 13 or above, the screen size changes when the orientation changes, so this still causes the activity to be destroyed and started when orientation changes.
Simply add the "screenSize" attribute like I did below:
<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize">
</activity>
Now, when you change orientation (and screen size changes), the activity keeps its state and onConfigurationChanged() is called. This will keep whatever is on the screen (ie: webpage in a Webview) when the orientation changes.
Learned this from this site:
http://developer.android.com/guide/topics/manifest/activity-element.html
Also, this is apparently a bad practice so read the link below about Handling Runtime Changes:
http://developer.android.com/guide/topics/resources/runtime-changes.html
You just have to go to the AndroidManifest.xml and inside or in your activities labels, you have to type this line of code as someone up there said:
android:configChanges="orientation|screenSize"
So, you'll have something like this:
<activity android:name="ActivityMenu"
android:configChanges="orientation|screenSize">
</activity>
Hope it works!
<activity android:name="com.example.abc"
android:configChanges="orientation|screenSize"></activity>
Just add android:configChanges="orientation|screenSize" in activity tab of manifest file.
So, Activity won't restart when orientation change.
It's my experience that it's actually better to just deal with the orientation changes properly instead of trying to shoehorn a non-default behavior.
You should save the image that's currently being displayed in onSaveInstanceState() and restore it properly when your application runs through onCreate() again.
This solution is by far the best working one. In your manifest file add
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="your activity name"
android:label="#string/app_name"
android:screenOrientation="landscape">
</activity
And in your activity class add the following code
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//your code
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//your code
}
}
In manifiest file add to each activity this. This will help
android:configChanges = "orientation|keyboard|keyboardHidden|screenLayout|screenSize"
add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities tags in manifest.
Just add this to your AndroidManifest.xml
<activity android:screenOrientation="landscape">
I mean, there is an activity tag, add this as another parameter. In case if you need portrait orientation, change landscape to portrait. Hope this helps.
just use : android:configChanges="keyboardHidden|orientation"
As Pacerier mentioned,
android:configChanges="orientation|screenSize"
All above answers are not working for me. So, i have fixed by mentioning the label with screenOrientation like below. Now everything fine
<activity android:name=".activity.VideoWebViewActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"/>
http://animeshrivastava.blogspot.in/2017/08/activity-lifecycle-oncreate-beating_3.html
#Override
protected void onSaveInstanceState(Bundle b)
{
super.onSaveInstanceState(b);
String str="Screen Change="+String.valueOf(screenChange)+"....";
Toast.makeText(ctx,str+"You are changing orientation...",Toast.LENGTH_SHORT).show();
screenChange=true;
}
Prevent Activity to recreated
Most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities wonβt be recreated and all your views and data will still be there after orientation change.
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
this is work for meπππ
Save the image details in your onPause() or onStop() and use it in the onCreate(Bundle savedInstanceState) to restore the image.
EDIT:
More info on the actual process is detailed here http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle as it is different in Honeycomb than previous Android versions.
I dont know if the a best solution, but i describe it here:
First of all, you need certificate with you class Application of your app is in your manifest of this:
<application
android:name=".App"
...
Second, in my class App i did like this:
public class App extends Application {
public static boolean isOrientationChanged = false;
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onConfigurationChanged(#NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ||
newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
isOrientationChanged = true;
}
}
}
Third, you need to set a flag to Orientation Change, in my case, I always set it when the previous activity within the app navigation is called, so only calling once when the later activity is created.
isOrientationChanged = false;
So every time I change the orientation of my screen in that context, I set it every time it changes this setting, it checks if there is a change in orientation, if so, it validates it based on the value of that flag.
Basically, I had to use it whenever I made an asynchronous retrofit request, which he called every moment that changed orientation, constantly crashing the application:
if (!isOrientationChanged) {
presenter.retrieveAddress(this, idClient, TYPE_ADDRESS);
}
I don't know if it's the most elegant and beautiful solution, but at least here it's functional :)
Add this code after the onCreate ,method in your activity containing the WebView
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}
I am creating an Android app in which I'm drawing a view on a canvas. When the device's orientation changes, the activity restarts. I don't want it to.
How can I avoid restarting the activity when the orientation changes?
There are various ways to do it, but as given here, using
android:configChanges="keyboardHidden|orientation|screenSize"
allows you to listen for the config changes. You then respond to these changes by overriding onConfigurationChanged and calling setContentView.
This is the way I've been doing it, but I'd be interested to know other people's thoughts.
Define your activity in the AndroidManifest.xml like this:
<activity
android:name="com.name.SampleActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:icon="#drawable/sample_icon"
android:label="#string/sample_title"
android:screenOrientation="portrait" >
</activity>
Check in your android manifest file that you have written android:configChanges="orientation" on the activity..
I tried to write android:configChanges="keyboardHidden|orientation|screenSize" in activity tag but in not works.
I tried a lot of methods but nothing works until I added android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities and it works perfectly.
For xamarin users,
To avoid the application restart on orientation change in Android, add this
ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize
to Activity attribute of all Activity classes.
For example, below is my demo code
[Activity(Label = "DemoApp", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
//Some code here
}
}
Add android:configChanges="keyboardHidden|orientation" to your activity
I would recommend using Fragments. You can simply use setRetainInstance(true) to notify that you want to keep your fragment.
Add this to all of your activities in the manifest.
android:configChanges="orientation|screenSize"
Example:
<activity android:name=".activity.ViewActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"/>
TO avoid restart on keyboardHidden|orientation - How to disable orientation change in Android?Please follow Android API guide - Handling Runtime Changes Using the Application Class - Activity restart on rotation Android
Declare this in your AndroidManifest.xml
<activity android:name=".complex_examples.VideoPlayerActivity"
android:configChanges="keyboard|keyboardHidden|orientation
|screenSize|screenLayout|smallestScreenSize|uiMode"
android:launchMode="singleTop"/>
But take care, Android Developers Documentation says that you should do it only if there is no better options left.
Note: Using this attribute should be avoided and used only as a last
resort. Please read Handling Runtime Changes for more information
about how to properly handle a restart due to a configuration change.
If you are sure about doing it, you can handle the configuration changes by your self in onConfigurationChanged() method.
To stop from destroying the activity on rotation
`android:configChanges="keyboardHidden|orientation|screenSize"`
just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file
For me occur when change the night mode, only write this in the manifest:
android:configChanges="uiMode"
Put this under AndroidManifest.xml
<activity android:name=".MainActivity"android:configChanges="orientation|screenSize">
please let me know if it worked(It worked for me, I'm new to Android studio)
I saw this code on web.
If you are using a "TextView" field to output answers it will reset on orientation changes.
Take note that "EditText" fields do not reset on orientation changes. with no additional code needed.
However, "android:inputType="none" does not work.
"android:editable="false"" works but its depreciated.
I'm using the latest version of Android Studio (Bumblebee)
If you use this code in your Android Manifest file in Android Studio Bumblebee:
<activity android:name=".MainActivity"android:configChanges="keyboardHidden|orientation|screenSize">
You need additional code as this code above messes your layout up when you change orientation. However, it keeps the text/numbers in your choice of text input fields respectively.