Orientation to Android - on-screen download

im going to re-ask this question because SO is very bad information, there it really is depressing. the story that I do not want something to change or happen when the orientation of the device changes. 2 of the most popular “solutions” posed by this problem:

1. You can block activity in one orientation by adding android: screenOrientation = "portrait" (or "landscape") to your manifest.

2. You can tell the system that you are going to process the screen changes for yourself by specifying android: configChanges = "screenOrientation" in the tag. This way, the activity will not be recreated, but instead a callback will be received (which you can ignore because it does not suit you).

None of these works. so let me explain in more detail my specific problem ...

I am experimenting with a VERY simple application as a training exercise. I have a text file on the server. A text file has 1 thing in it: one integer on one line. this number is the number of image files also stored on this server, and all images are indicated by 0.jpg, 1.jpg, 2.jpg, etc.

ALL of my code is in the onCreate method of my activity (for example, I said that this is a simple application).

The application performs the following actions:

reads a number from a text file. generate a random number from zero to the number in the file. loads a random image into an image using a random number in the url.

when the screen rotates, I do not want all this to be repeated again. I just want NOTHING to happen ... except that the screen must obviosuly rotate and the image must scale to fit, which is what it does. but every time the screen rotates all these runs of code, and a new random image separates. can someone please give me a simple solution with working code to fix this? I will find out by seeing this if you cannot provide an example of code that it will not help.

early.

ps ... im is not looking for another way to do what it does, this is not the point. im looking for FIX the way im is currently doing this.

+6
source share
5 answers

A very similar question: Do not restart the application when changing orientation

In any case, first of all, you should change the code from onCreate to another method and divide it into "create a random number and download the image from the network" and "set the image for viewing".

You have a bitmap in the action that you check to see if it is zero every time the action starts.

If so, do it all. If this is not the case, simply install ImageView or something else using Bitmap.

To avoid the destruction of the bitmap during its rotation, 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; } 

It works, I'm 100% sure.

+3
source

A solution using android:configChanges="orientation" will not work if the application does not have API level 12 or lower.

At API level 13 or higher, the screen size changes when the orientation changes, so this still leads to the destruction of the activity and its beginning when the orientation changes.

Just add the screenSize attribute as shown below:

 <activity> android:name=".YourActivityName" android:configChanges="orientation|screenSize"> </activity> 

Now that your orientation changes (and the screen size changes), the activity retains its state and is called onConfigurationChanged (). This will keep everything on the screen (i.e.: webpage in webview) as the orientation moves.

Learned about this from this site: http://developer.android.com/guide/topics/manifest/activity-element.html

Also, this seems to be bad practice, so read the link below on how to handle runtime changes:

http://developer.android.com/guide/topics/resources/runtime-changes.html

+10
source

Save the image details in onPause() or onStop() and use it in onCreate(Bundle savedInstanceState) to restore the image.

More details about the real process are described in detail here http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle , since it differs from the cellular than previous versions of Android.

0
source

For example, add a manifest:

 <activity android:name=".Explorer" android:label="@string/app_name" android:configChanges="orientation" android:launchMode="standard"> </activity> 

and then just override the default function:

  @Override public void onConfigurationChanged(Configuration _newConfig){ super.onConfigurationChanged(_newConfig); int height = getWindowManager().getDefaultDisplay().getHeight(); int width = getWindowManager().getDefaultDisplay().getWidth(); if(width > height){ // layout for landscape }else{ // layout for portrait } } 

NOTE. If you override this function, you must redo the layout yourself. This is the only way to do what you want. An Android approach to fixing your screen size is to remove all activity and raise it again with new dimensions. If you do not want it to hit Activity, then you will have to handle screen resizing. There are no other options for this.

EDIT: In this particular application, I have a table that displays in the portrait, and a chart that displays in the landscape. So I do this:

 void showDataView() { setContentView(mainView); currentView = mainView; } void showGraphView() { if(currentView == mainView){ if(graphView == null){ setContentView(R.layout.graphview); graphView = (GraphView)findViewById(R.id.graphview); } setContentView(graphView); currentView = graphView; graphView.setDataToPlot(DATA_TO_PLOT); graphView.clear(); } } @Override public void onConfigurationChanged(Configuration _newConfig){ super.onConfigurationChanged(_newConfig); int height = getWindowManager().getDefaultDisplay().getHeight(); int width = getWindowManager().getDefaultDisplay().getWidth(); if(width > height){ showGraphView(); }else{ showDataView(); } if(graphView != null) graphView.setGraphWidth(width); } 

Every time the orientation changes, I switch the presentation of the content from one to another. In your case, you can simply invalidate current root view and see if it will be redrawn correctly.

That's why you want the platform to take care of this, so you don't do this shit. You would be better off using onSaveInstanceState to store specific data, then check onCreate for that data and then use it. See http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges for information on this.

0
source

what worked for me declares one of the class variables to be static. it turns out that the static class variables are initialized once when the application loads, but when the application restarts, they will not be initialized again.

these variables may contain a state or object that you do not want to reinitialize. I can’t say that it’s not very elegant, but tested and works for me.

0
source

Source: https://habr.com/ru/post/887464/


All Articles