im going to re-ask this question because there is SO much bad information out there its really depressing. the short story is that i dont want anything to change or happen when the device orientation changes. 2 of the most popular "solutuions" given to this problem are:
1.You could lock the activity in one orientation by addingandroid:screenOrientation="portrait" (or "landscape") to in your manifest.
2.You could tell the system that you meant to handle screen changes for yourself by specifying android:configChanges="screenOrientation" in the 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).
NEITHER of those work. so let me explain my particular problem in more detail...
i am experimenting with a VERY simple app as a learning exercise. i have a text file on a server. the text file has 1 thing in it: a single integer on a single line. this number is the number of image files also stored on this server, and the images are all named 0.jpg, 1.jpg, 2.jpg etc.
ALL of my code is in the onCreate method of my activity (like i said its a simple app).
the app does the following when it runs:
reads the number from the text file. generate a random number from zero to the number in the file. loads a random image into an imageview by using the random number in the URL.
when the screen rotates i do not want all of that to happen again. i simply want NOTHING to happen... except that the screen should obviosuly rotate and the image should scale to fit, which it does. but every time the screen rotates all of that code runs and a new random image is selcted. can someone please give me a simple solution with the working code to fix this? i learn by seeing so if you cant provide a code example it wont help.
thanks in advance.
ps... im not looking for a different way of doing what im doing, thats not the point. im looking to FIX the way im currently doing it.
The solution using android:configChanges="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 your 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 chagnes.
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
A very similar question is here: Don't reload application when orientation changes
Anyway, first of all you should change the code from onCreate to another method and divide it by "create random number and load image from the net" and "set image to view".
You have a Bitmap on the activity, that you verify if it's null everytime the activity starts.
If it is, do all. If it's not, just set the ImageView or whatever with the Bitmap you have.
To avoid destroying the Bitmap when it rotates use:
Bitmap image = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
image = (Bitmap) getLastNonConfigurationInstance();
if(bitmap == null){
image = downloadImage();
}
setImage(bitmap);
}
@Override
public Object onRetainNonConfigurationInstance() {
return bitmap;
}
This works, i'm 100% sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With