My app can only be used in landscape.
But i noticed when setting android:screenOrientation="landscape"
in my manifest the onPause is called twice.
Probably all activity livecycles will get called twice.
How can i avoid this, and is this normal?
Edit
I use a android annotations library (https://github.com/excilys/androidannotations).
Here is my activity declaration in my manifest.
<activity
android:name="MyActivity_"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan" />
Related
I'm experiencing a strange issue on a new app I'm currently developing, where if I send the app to background (Home button), and then relaunch the app using the App Icon, all activities, except for the root activity are destroyed (on log I get OnDestroy for all activities, except for the root activity where OnResume is called).
If I send it to background and resume it from the current tasks, it resumes the app.
I have added android:launchMode="singleTask" to all the activities on the app manifest but it made no difference.
Every activity is started normally: startActivity(new Intent(this, nextClass));
Is there any way to resume the app when pressing the App Icon instead?
AndroidManifest.xml
<application
tools:replace="android:icon"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.example.game.classes.App">
<activity
android:name=".LaunchScreenActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity
android:name=".PlayActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize" />
.
.
.
EDIT: Explanation to as not duplicate of App restarts rather than resumes
The difference between that situation and mine is that, on that case the main activity is launched on top of the other activities (you can press back to return to previous running activities). In my case the other activities are completely destroyed except for the launch activity which is resumed.
After days searching for a solution.. I finally found the problem.
The issue was adding android:launchMode="singleTask" to every activity.
I removed it from all activities except from the root activity and now it is working as I wanted.
I want to run my app in portrait mode, I'm aware this isn't best practice but there are reasons to do this. and while I have disabled the rotation it still is able to rotate on some views will it doesn't on others.
I have this part of code in my Android Manifest:
<activity
android:name="<name>.app.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|keyboard|locale|orientation"
android:screenOrientation="portrait">
I'm using fragments to show different containers depending on user input.
this is the only activity that has fragments.
I have tried a few solutions on this site. including setting portrait mode on by code
You can do it something like below.
After rootView in your java add this line
getActivity().setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // programmatically
For example:
View rootView = inflater.inflate(R.layout.activityxml, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
And also in your manifest change it:
android:configChanges="orientation|keyboardHidden"
as
android:configChanges="keyboardHidden"
<activity
android:name="com.test.activity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden" >
try this code
<activity android:name="com.myActivity" android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
>
</activity>
use below code before super.onCreate.first line of code.it force activity to portrait(of course both side of portrait)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
the code that you entered android:configChanges="screenSize|orientation" just force activity to don't miss it's stats on rotate(onCreate call just once and if rotate onPause will call)
add Android Manifest:
tools:ignore="LockedOrientationActivity"
android:screenOrientation="portrait">
Is there a way I can start activity in a specific orientation (programmatically) without having it to be recreated again? Right now if I call setRequestedOrientation from the the OnCreate and the orientation is different than the current then the activity is destoryed and restarted again.
Thank you
For me solution was to lock orientation of Activity
android:screenOrientation="locked"
, and now it depends only on setRequestedOrientation():
<activity android:name=".SomeActivity"
android:label="#string/app_name"
android:screenOrientation="locked">
No restarting anymore.
You can set your orientation in Manifest file
<activity android:name=".SomeActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
If you don't want activity to restart, add configChanges for the activity in AndroidManifest.xml
<activity
android:name="com.myexample.SplashScreen"
android:configChanges="orientation|screenSize"
android:label="#string/app_name" >
and then call setRequestedOrientation to set orientation programtically.
But if you want to lock the activity to 1 orientation. It is good to set it in Manifest by adding android:screenOrientation="portrait"
I want to use all activities in my form in landscape or portrait.
When user select orientation - this is valid for all activities.
Tried with "behind" option orientation. According to Google - orientation will depend on previous activity.
My first activity use setRequestedOrientation to set selected from user orientation.
Next activities have to follow same orientation.
Do I have to put setRequestedOrientation in their code too? Or really on 'behind' parameter in manifest? Putting setRequestedOrientation may be cause onCreate again?
UPDATE:
Tried "portrait" and setRequestedOrientation() - result is onCreate was called 2 times.
Problem is in next activity -> because of "portrait" in first activity - android started next activity with same orientation. It ignores "landscape" orientation which was set by me.
If you want to have fixed orientation for your activities then you can use-
android:screenOrientation="portrait"
android:screenOrientation="sensorPortrait"
as an attribute to that activity in that manifest. But if you want to set the orientation runtime depending upon what was the previous orientation while launching the application, you need to check for the previous orientation in onCreate() and then set it to that value programmatically there itself using setRequestedOrientation()
UPDATE:
As pointed by #s.co.tt use android:screenOrientation="sensorPortrait" for a better support on tablets.
For more details on the different values for android:screenOrientation and what each of them do, look at the docs:
https://developer.android.com/guide/topics/manifest/activity-element.html#screen
<activity
android:name=".Android_mobile_infoActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To handle the changes in orientation, add the following line in AndroidManifest.xml:
android:configChanges="orientation|screenSize"
under <activity> tag as shown below:-
<application
android:allowBackup="true"
----------
---------- >
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
----------
</activity>
----------
</application>
Note: Whenever there is a change in the configuration such as orientation, screenLayout, screenSize, etc., the activity is restarted and its onCreate method is called. To prevent this, we must handle any changes in configuration.
Do it programmatically, for example in an Activity base class
I tryed its working mobile and tablet. use anyone portrait or landscape.
#Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
manifest.xml inside application tag
<activity
android:name=".Login_Activity"
android:label="#string/app_name"
android:screenOrientation="portrait" />
or
<activity
android:name=".Login_Activity"
android:label="#string/app_name"
android:screenOrientation="landscape" />
In Android studio 3.6.0
android:screenOrientation="portrait"
will give you error you have to either specify
android:screenOrientation="fullSensor" or android:screenOrientation="unspecified"
fullSensor Means either you have ON the "Rotate off" or not it will change the orientation based on you move the phone
unspecified Means if you have ON the Rotate off then it will stay only in that orientation and if not then it will change the orientation based on you move the phone.
From my experience I would recommend to setRequestedOrientation in every activity in onCreate method, onCreate won't be called again it's safe.
For now I put check in onCreate:
m_bSkip = (this.getRequestedOrientation() != MyApp.sInstance.GetScreenOrientation());
if (m_bSkip)
return;
When I enter oncreate and screen orientation is not desired - exit.
When I enter in onCreate and screen orientation is desired one - continue with initialization.
This fixes situation without need of keeping async task related to activity and check for new activity.
Of course all functions: onStart,onResume,onPausde,onStop... have to check this flag to avoid null pointer exception.
go to Android Manifest editor , down there you will see MainActivity.java
click on it , to the right - you will see a new window: scroll down and choose select orientation - "portrait"
I have tried to freeze orientation using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Although the display stays in portrait orientation, the activity is still recreated. Any ideas how to solve this?
How can the orientation of the application be locked such that the activity is not recreated on orientation change?
First, don't use setRequestedOrientation() if you can avoid it. Use the android:screenOrientation attribute in your <activity> manifest element instead.
Second, you will also need android:configChanges="keyboardHidden|orientation" in your <activity> manifest element to prevent the destroy/recreate cycle.
A more specific example of the activity section of the AndroidManifest.xml for portrait orientation:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where android:screenOrientation sets the initial orientation and android:configChanges voids the events that triggers the corresponding lifecycle methods on screen changes.
Try this:
1.- Set the desired screen orientation in your AndroidManifest.xml
android:screenOrientation="portrait|landscape"
It should look like this:
<application
android:allowBackup="true"
android:icon="~icon path~"
android:label="~name~"
android:supportsRtl="true"
android:screenOrientation="portrait"
android:theme="#style/AppTheme">
</application>
2.- Add this to your onCreate void(or wherever you want) in your java Activity File(Example: "MainActivity.java"):
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
It should look like this:
protected void onCreate(Bundle savedInstanceState) {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);}
Now the screen wont move even if the Screen Rotation is on in the Device.
The best solution is to use the saved instance.
If you are locking the the screen orientation then it means you are forcing the user to use the app according to constraints set by you. So always use onSaveInstanceState. Read this link: http://developer.android.com/training/basics/activity-lifecycle/recreating.html