Android的 - 屏幕方向重新加载活动加载、屏幕、方向、Android

2023-09-05 09:41:29 作者:人心太拥挤

即时通讯将重新提出这个问题,因为有这么多的不良信息在那里它真的去pressing。短篇小说的是,我不希望任何更改或发生在设备方向更改。 2给这个问题的最流行的solutuions是:

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。你可以锁定一个方向的活性addingandroid:screenOrientation =肖像(或风景),在你的清单。

1.You could lock the activity in one orientation by addingandroid:screenOrientation="portrait" (or "landscape") to in your manifest.

2。你能告诉你的意思来处理自己的屏幕变化,通过指定的Andr​​oid系统。这样的活动将不会被重建,但会收到一个回调,而不是(你可以忽略,因为它不是对您​​有用)。

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...

我尝试用一​​个非常简单的应用程序作为一个学习锻炼。我有一个服务器上的一个文本文件中。该文本文件中有1的事情吧:在一行上一个整数。这个数字图像文件的数量也存储在该服务器上,并且图像都被命名​​为0.jpg,1.JPG,2.JPG等。

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.

我所有的code是在我的活动的onCreate方法(就像我说的它的一个简单的应用程序)。

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:

读取文本文件的数量。 产生一个随机数从零到文件中的数。 通过使用在URL随机数加载随机图像成imageview的

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.

当屏幕旋转,我不希望这一切再次发生。我只是想没什么可发生......除了屏幕应该obviosuly旋转和图像应该缩放以适应,而它的作用。但每次屏幕旋转,所有code运行和一个新的随机图像selcted的。可有人请给我的工作code一个简单的解决方案来解决这个问题?我学会通过观察,所以如果你不能提供一个code为例,它不会帮助。

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.

在此先感谢。

PS ......我不是在寻找做即时通讯做了不同的方式,这不是重点。即时寻找到FIX的即时通讯方式正在这样做。

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.

推荐答案

一个非常类似的问题在这里:Android - 不要重新加载应用程序时,方向更改

A very similar question is here: Android - Don't reload app when orientation changes

不管怎样,首先你应该改变从的onCreate的code另一种方法,并通过创建从网上随机数和负荷的形象和设置图片查看分吧。

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.

如果是,尽一切。如果不是,只是设置的ImageView或任何与你有位图。

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;
}

这工作,我敢100%肯定。

This works, i'm 100% sure.