Disable Screen Orientation Dynamically In Android(Java) - java

I want to lock my application to portrait mode for all the activities. I know this can be done by setting android:screenOrientation="landscape" in activity tag in manifest. but I have lots of activities, so I was wondering is there any way to do it on application level either by some xml tag in manifest or in code anyhow, so I wont have to define it in all the activity tags.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Use this method in the BaseActivity. And extend this activity for every other activity in your application.

Related

How can I add Landscape (Rotation) in all activity in one shot?

I've one major issue. I built one chat app from scratch using Firebase, Java and Android, which contains many activities and class. Now suddenly I found that I forgot to set UI for Landscape mode (which is like default tablet mode). I opened my app and rotate to landscape, and UI looks very bad, even some part is not visible. I'm actually planning to publish on play store just for learning purpose. So is there any easy way to do this?
Should I restrict, so that user not able to rotate the screen for every activity like below is mation in either java or xml through?
In XML:
<activity android:name=".SomeActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
In Java:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I also don't know that how can I do this for all activity? Should I have to crate duplicate activities for all activity? and how can I do this? where can I attach? I really don't have any idea about this. Please help me.
Studio generates layout for you just adjust the generated layout as per need and that layout will be used automatically
Should I restrict, so that user not able to rotate the screen for
every activity like below is mation in either java or xml through?
You shouldn't restrict user to one orientation, If design specs of you layout is ideal for landscape then go for it
I also don't know that how can I do this for all activity? Should I
have to crate duplicate activities for all activity? and how can I do
this? where can I attach? I really don't have any idea about this.
Please help me.
With manifest solution, yes, you have to do this for every activity entry in manifest file.
either do it via manifest or via xml.
To set orientation of all activities, you can create a base activity and extend all activities from it where your base activity set the orientation. e.g
// no need to set orientation in manifest
class BaseActivity extends AppCompactActivity{
oncreate(...){
super.oncreate(..);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
class YourOtherActivities extends BaseActivity{
oncreate(...){
super.oncreate(..);
setContentView(..);
}
}

Up Navigation: From Activity to Fragment: Android Studio

The basic navigation of my app is a tab bar within one activity in which each tab is its own fragment. I am trying to get the back button to work to go back to the last tab it was on. I can get the back button to work/appear if the activities are going to the first tab by using this in the java class of the activity:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and this in the AndroidManifest.xml:
<activity android:name=".CreateNewPost" android:parentActivityName=".MainActivity"></activity>
This works fine, however when I go to a new activity from a button within the other tabs, if I use this same method it goes to tab1. How do I tell it to go to a specific tab or the last active fragment?
Any suggestions would help!
When you launch a new activity the old one either paused or stopped , as in either OnPause() or OnStop() method has been called. Then when you navigate back to a previous activity it is either started (OnStart()) or resuemd (OnResume()). And sometime the activity is destroyed and created again when navigating back to it using OnDestroy() and OnCreate() respectively.
The most guaranteed way that I can think of is to store the state of the first activity somewhere persistently and when the user navigates back to it check what has been stored and show that fragment.
You can use SharedPreferences to store this kind of data, see the google developer documentation on that here. Or onSaveInstanceState(Bundle savedInstanceState) check out this SO question on saving activity state. Although from the answers it is clear that onSaveInstanceState should not work in your specific case, but you should look at it any way because it is useful in other cases like changing screen orientation for example.

android Change orientation removing button

I've set android:configChanges="orientation|screenSize" on an activity to stop it restarting the activity everytime the orientation changes. Now this works fine but I think it's stopping the correct layouts from being used.
E.G. I have different layout folders for different orientations and sizes of screen. So if I start the activity in portrait, when I change the orientation to landscape, it's not using my landscape layout.
Also if I start the activity in landscape, when I change the orientation to portrait, it's not using my portrait layout.
Basically what I want the app to do is not start the activity again once the orientation changes, but use the correct layout when the orientation is changed!
I was thinking I could use the onConfigurationChanged method to explicitly change the layout in code?
Thanks for any input
when you use android:configChanges="orientation|screenSize" this tells to Android that you will maintain these changes by yourself - this means you have to change your layout (setContentView) and initialize it manually (set values of controls - EditTexts, Spinners etc.)
So What I've ended up doing is keeping android:configChanges="orientation|screenSize" in the manifest file. Doing it this way stops me being able to use my correct layout folders for portrait and landscape.
To overcome this issue, I override the onConfigurationChange method to set the correct information I needed for the activity to run as expected once the orientation changes. below is the code I've used:
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.hearing_test);
//Any other information needed for the activity to work correctly
}
Thanks for the help and guidance #mihail, helped me get to the bottom of the issue.
That is exactly why the Activity is destroyed and recreated: to apply new resources.
Do not use android:configChanges="orientation" if you have different layouts for portrait and landscape mode.

Activity keeps restarting when orientation changes

how do you keep the activity from restarting when the screen rotates or when then user slides the keyboard on the phone? Is this possible? Is there a work around or something? All relevant answers are appreciated.
You can do this by declaring a specific attribute in your activity element in your manifest.xml. The element in question is called android:configChanges, and you need to register the string value of orientation.
<activity android:name=".MyActivity"
android:configChanges="orientation"
android:label="#string/app_name">
From the documentation:
Now when one of these configurations change, MyActivity is not
restarted. Instead, the Activity receives a call to
onConfigurationChanged(). This method is passed a Configuration object
that specifies the new device configuration. By reading fields in the
Configuration, you can determine the new configuration and make
appropriate changes by updating the resources used in your interface.
At the time this method is called, your Activity's Resources object is
updated to return resources based on the new configuration, so you can
easily reset elements of your UI without the system restarting your
Activity
So doing this will cause your Activity to not restart, and will also callback to onConfigurationChanged() so that you can handle the change yourself.
If you read the documentation here, you will see that you can specify the following in your manifest:
<activity ...
android:configChanges="orientation">
Once you have this you can implement the onConfigurationChanged() method to receive notifications about orientation change, or just use the base class's implementation.

Saving Application State in Android

I am writing an app where I get all the data from the rest call and display all the data in a custom component list(based on Linear Layout) which is added to a LinearLayout. I write this code in onCreate of the activity.
The problem is when I switch activity using startActivity, and come back to the calling activity (using startActivity) then onCreate is called again. I see onPause, onStop called when I call other activity.
Is there any way that I can save the application's state?
Check this question: "How do I save an android application state".
Edit:
You can also avoid some calls to onCreate() by adding a
android:launchMode="singleTask"
to your Activity in the AndroidManifest.

Categories

Resources