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.
Related
Here is my issue. I have a requirement to add a search dialog to an Android app. I have found the way to add a search view component, but not the search dialog. I know from the Android Developers docs that I am supposed to use onSearchRequested() to make it appear, but I don't quite understand what that means. Is the search dialog supposed to be tied to my own UI component that will invoke this method or am I missing something? I haven't managed to read all the docs on searching, but I was hoping someone may point me where I missed something important.
I am not well versed in Android development, only a few small projects. I am more of a Flutter dev when creating a mobile app, but I don't have the option this time around.
Below is the screenshot of what the Android Developers docs show as search dialog and the comparison to search view.
Based on the documentation, the steps for invoking a Search Dialog are:
create an Xml file (which you already did for search widget)
Declaring a searchable activity: create an actiivty and notify the Manifest that it gets the search results (you did that too)
3. At the Manifest, decalre some other activity (MainActivity for examlpe) to be able to invoke the search dialog. Add this line inside <activity> tag:
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
(android:value is the name of the Activity that gets the searcg results, which you defined at step 2)
4. At that Activity, create a search button etc and invoke the Serach Dialog like this:
onSearchRequested();
Very simple actually :)
The search words stored in intent.getStringExtra(SearchManager.QUERY) as a String, you get it at onCreate() of SearchableActivity class. From this point you should handle the search query and results logic...
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(..);
}
}
.
I am doing reverse-engineering of applications.While reverse-engineering an app i needed to change the titles of each activities in an app that i got .I have a problem regarding activity titlebar.
Actually the developers of this app have set many of the titles from androidManifest.xml file using
<activity android:name="com.example.dmo.MainActivity" android:label="first"></Activity>
I changed the title of each activity by putting setTitle(" say abc"); in onCreate() of each activity and title is successfully changed, But problem is that while loading activity it shows the titlebar with the title from androidManifest.xml file i.e. android:label="first".
This title appears for some miliseconds (time increases if activity takes more tome to load), and then when my activity loads completely, title changes to the text i passed in setTitle() in onCreate().
This is somewhat similar to my problem.
I am not able to find solution since i have a strict restrictions that i can not make changes in any of the .xml fiels in an applicaion. I only allowed to change the java code and add some new java code if necessary.
I searched a lot but all solutions i found suggested changes in xml files; and some other solutions those suggested using code did not worked.
So is there any solution that does not require manual hard code xml changes directly. Changing xml using code is acceptable but not hard code changes like editing their original manifest file to :`<activity android:name="com.example.dmo.MainActivity" android:label="say abc"></Activity>`
Any AspectJ solution is also acceptable(i.e. some pointcut to some method that can do the work for me), if someone knows for the same.
I also want to know that, which class or api is responsible to load title bar before activity loads? ,so that if possible i can make changes in that .
Any help would be very appreciable . .Thank u in advance . .
Then the answer is you can't. This is the Application theme that is shown and it can be changed only in manifest. The screen you see is the Activity screen in state when the Application is not yet loaded and before any code in Activity onCrete() is executed.
Did you try apktool? You should be able to get editable manifest using it.
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.
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.