in my application (minSdkVersion 17, but I am flexible concerning this), I have a single activity. It displays some kind of timetable. I want to let it show the current day and scroll to the correct time.
This functionality is already working perfectly fine.
I want to do this everytime when the app is started, or everytime when the app becomes active again (e.g. after pressing the home button and just clicking the app icon again without killing the app inbetween).
But I do NOT want to do this, when the device is rotated.
For example, I have a method:
private void scrollToNow() {
// do the calculation and scroll all views to the correct position
// this is code I already have fully functional!
}
And I want to execute it every time the activity gets active again, but NOT on orientation change.
I tried a lot of combinations (in onStart, onResume, onCreate), but none of them did the trick. I tried to build a solution using the savedInstanceState but failed.
Anybody got some tips or useful links for me?
Thanks alot,
Oliver
Please, think twice before you decide to check configuration changes manually.
If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
Try to save some flag in onSaveInstanceState and in onCreate check if there is savedInstanceState, then save it as field for example. This will give you information about does activity is 'recreated' or not. Then in 'onResume' check is this flag set or not. If not, then you are not 'recreated' your activity, so you can invoke scrollToNow()
I dont want to recreate layout when screen orientation change
Did you tried to add this line to your AndroidManifest.xml android:configChanges="screenLayout|screenSize|orientation" like that :
<activity
android:name="youractivity"
android:configChanges="screenLayout|screenSize|orientation"
android:label="#string/title_activity_create_note"
android:theme="#style/AppTheme"
android:windowSoftInputMode="stateAlwaysHidden" />
For more information you can take a look at : manifest/activity-element in that page go to android:configChanges section and read please .
When adding this you must handle onConfigurationChanged() method by yourself instead of Android System.
Please read carefully Note section in that page :
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.
Also i am using this in my application maybe you can try this too : android:screenOrientation="nosensor" in that page you can find information about it too.
Related
No, not a regurgitation of the old question, please, bear with me. I don't want to avoid activity recreation in general (no android:configChanges in manifest), I don't want to fix my orientation permanently (no android:screenOrientation in manifest) just because I'm lazy to implement instance saving.
I have an app with three possible settings the user can make: 1. automatically changing layout on orientation, as per normal, 2. fixed portrait, 3. fixed landscape. It makes sense in my case because the portrait and landscape displays show different functionality and the user might want to restrict to just one. Doesn't have to but has the possibility.
The app works just fine. I read the preference setting in onCreate and call setRequestedOrientation if I'm in one of the fixed modes. I let the system handle the orientation changes, I don't ask for handling the changes myself.
The only performance problem is that when, for instance, the app is started in the device's portrait position but fixed to landscape, onCreate will be called twice, once for the original startup, once for setRequestedOrientation. It works flawlessly, I handle it perfectly but there is a performance penalty, the activity appears with an obvious delay. (With screenOrientation fixed in the manifest, only for the purpose of testing, the startup looks much better, with only a single call to onCreate).
So, what I'm looking for is a kind of code equivalent of the manifest screenOrientation setting. I can't and don't want to specify it in the manifest but calling it from onCreate is already a bit late for performance.
Try to read users preference in Application Class, and in the onCreate setContentView on the basis of that value.
public class MyApp extends Application{
public void onCreate(){
// get user preference here in a global variable
}
}
Set this as application class in Manifest.
Then in your activities onCreate, use this value to determine layout before setContentView().
did you try this?
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode" />
This configChanges line on the manifest file should avoid the re-calling to the onCreate method :)
android: data gets lost when phone changed to landscape mode of TIC-TAC-TOE game
I have created an android app it works perfectly fine,but when i change the position of my phone to landscape mode all the all data gets reset that is all the text on the buttons of the game gets replaced by blank values.
Since i am new to android programming i am unable to sort this problem,
please help. thank you.
I'm not posting the XML code and java code cause its very big,
if you want to see the XML code or java code,then please comment.
You need to override the onSaveInstanceState method. When that method is called, save the data you want to keep to the bundle object. Then in onCreate you should be checking if bundle != null), and if that is true (meaning you saved stuff in the onSaveInstanceState method, update your variables based on values in the bundle.
From the Android documentation, you should avoid using the android:configChanges property:
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.
Regarding that you might have already handled the savedInstanceState, then include
android:configChanges="orientation"
in the activity in android manifest.
May be it case for activity reload
there is a way you can try:Simply add the "screenSize" attribute like I did below:
<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize">
</activity>
more details: about it
http://developer.android.com/guide/topics/manifest/activity-element.html
and
http://developer.android.com/guide/topics/resources/runtime-changes.html
I have problem with my activity
when I change the phone's orientation, the activity runs the onCreate method agaian
(and my program downloads a file in onCreate)
how can I make activity not restart after orientation change ?
This is the default behaviour for Android Activities - They get recreated whenever you change the device configuration.
To change this behaviour, add this line to the Activity declaration in your AndroidManifest.xml:
android:configChanges="orientation|keyboardHidden|screenSize"
This will tell the system that you handle orientation changes for your Activity on your own.
android:configChanges is good, if you're not changing anything, when orientation of activity will be changed.
You can also read about handling orientation change in documentation:this and this
You should properly handle you configuration changes, you can use onSaveInstanceState to store information that given file was already downloaded - ie. with path to it. In onCreate Bundle savedInstanceState will be non null after configuration change, you can read information on your downloaded file from it.
If you will use android:configChanges, then it will not solve your problem. Once you go to other app, android might destroy your activity, when you will go back to your app, android will recreate it again - and you again will start downloading your file.
I have a simple WebView application which takes some time to load up when i change the orientation. In order to optimise i would like to get an understanding of the processes involved when changing orientation. I added dummy activity lifecycle methods with text being sent to logcat using the Log.d method. i however can not see any of these methods being used when i change the orientation.
I did a bit of digging and apparently i can also override the behaviour of the orientation configuration by using onConfigurationChanged, I coded the onConfigurationChanged method with some logcat statements but the program does not appear to go through this method as well.
Can you tell me how do i view the methods involved in configuration change so that i can optimise my code. as per this "article" configuration change should result in onDestroy being called, followed by onCreate. I cant see the onDestroy method executed, instead the program stops at onStop and does not appear to proceed further.
i checked in the logcat and each time i changed the orientation the WindowManager followed by the ActivityManager is fired.
can you guys tell me how to view the transitions? is there any setting i need to set someplace?
Android Baby,
Try adding OnPause and OnResume overridden methods and add your breakpoints to those methods. They will be definitely called on Orientation changes.
Have you added the android:configChanges to your manifest file?
Here the documentation says onConfigurationChanged will be called only if you have android:configChanges="orientation" in the manifest file. Otherwise Android itself will handle all config changes.
If, as described in the article you refer to, you have put the following line in your manifest:
android:configChanges="orientation|keyboardHidden"
then your activity won't execute onPause/Stop/Destroy as it finishes in one orientation and then restarts in the other, running through onCreate/Start/Resume.
This is because you have told the application that you will handle the orientation changes yourself when you insert that line. If you write an override for onConfigurationChanged(..), then it will run through that.
I have been going gaga to figure this out.
Although I have read a lot that on Orientation Change, Android kills an activity and starts it as a fresh one, and the only way to handle this is to save all the stuff inside onSaveInstanceState() and try to restore it inside onCreate().
But my activity does a lot and different kind of network activities at different times and if the orientation is changed when the network activity is being performed, I'll have to handle a lot of different and complex scenarios.
Is there any simple way to just point Android that this activity doesn't need to be redrawn at all when the orientation is changed so that it automatically saves all the data and re-uses it?
I wonder if there's any thing like that.
Yes, you can add attribute android:configChanges="orientation" to the activity declaration in the AndroidManifest.xml file.
EDIT:
The purpose of the android:configChanges attribute is to prevent an activity from being recreated when it's really necessary. For example the Camera application uses this attribute because it the camera preview screen mustn't be recreated when an orientation change happens. Users expect the camera preview to work without any delays when they rotate their devices and camera initialization is not a very fast process. So it's kind of a native behavior for the Camera application to handle orientation changes manually.
For most applications it doesn't really matter if an activity is recreated or not during orientation changes. But sometimes it's more convenient to persist an activity during this process because of slow activity creation, asynchronous tasks performed by an activity or some other reasons. In this case it's possible to tweak an application a little and to use the android:configChanges="orientation" attribute. But what is really important to understand when you use this tweak is that you MUST implement methods for saving and restoring a state properly!
So to sum up this answer, the android:configChanges can allow you to improve the performance of an application or to make it behave "natively" in some rare cases but it doesn't reduce the amount of code you have to write.
But my activity does a lot and different kind of network activities at different times and if the orientation is changed when the network activity is being performed, I'll have to handle a lot of different and complex scenarios.
Then move that logic out of the activity and into a service.
Yes, you can add attribute
android:configChanges="orientation" to
the activity declaration in the
AndroidManifest.xml file.
IMHO, it's better to declare
android:configChanges="orientation|keyboard|keyboardHidden"
About the blog post you gave the link in another answers. I guess here is the answer:
If your application doesn't need to
update resources during a specific
configuration change and you have a
performance limitation that requires
you to avoid the Activity restart,
then you can declare that your
Activity handles the configuration
change itself, which prevents the
system from restarting your Activity.
I spoke as well with an android developer about this problem. And he meant following. If you don't have different layouts for landscape and portrait orientation, you can easy use configChanges.
I solved my problem by adding this to my activity in my manifest file
android:configChanges="keyboardHidden|orientation|screenSize"
Just answered this question earlier: Android - screen orientation reloads activity
In your case you want to completely prevent Android from killing your Activity. You'll need to update your manifest to catch the orientation change, then implement the orientation change callback to actually do whatever you need to do (which may be nothing) when an orientation change occurs.
if you are doing a lot of networking inside Asynchronous task maybe you should use onRetainNonConfigurationInstance()
ans then get the data back in your onCreate() method like this tutorial or this
add android:configChanges="orientation" to your activity in manifest and add this code in your activity class and check..i hope it will help for you.
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
this method will be called when orientation is changed nothing else if u don't want to change anything let it be blank
android:screenOrientation="portrait" in the activity tag in the manifest will lock your orientation.
Check this link for more inforation.