传递额外设备和屏幕旋转 [英] Passing Extras and screen rotation
问题描述
这样的问题周期性地出现。如果这已经涵盖对不起之前,但我是一个新手,但没有找到合适的答案。它涉及的正确实施课程和活动之间的沟通。
This kind of questions appear periodically. Sorry if this has been covered before, but I'm a newbie and couldn't find the appropriate answer. It deals with the correct implementation of communication between classes and activities.
我做了一个画廊的应用程序。它有3个主要活动:主
之一,来搜索使用模式的文件名;一个拇指
之一,显示所有在GridView匹配模式为缩略图的图像,以及图片
的活动,打开一个全尺寸的图像,当你点击大拇指
拇指。我通过传递给图片
活动的意图
的文件名(阵列),而位置
(一个 INT
)在GridView被点击的拇指。
I made a gallery app. It has 3 main activities: the Main
one, to search for filenames using a pattern; a Thumb
one, that shows all the images that matched the pattern as thumbnails in a gridview, and a Photo
activity, that opens a full sized image when you click a thumb in Thumbs
. I pass to the Photo
activity via an Intent
the filenames (an array), and the position
(an int
) of the clicked thumb in the gridview.
这第三个图片
的活动只有一个观点就可以了:一个 TouchImageView
,我改编为$ P $根据在那里你shortclick的图像(左,右或中间)上pvious /下一个开关和变焦。此外,我添加了一个longclick监听器图片
来显示EXIF信息。
This third Photo
activity has only one view on it: a TouchImageView
, that I adapted for previous/next switching and zooming according to where you shortclick on the image (left, right or middle). Moreover, I added a longclick listener to Photo
to show EXIF info.
事情是工作,但我不开心的实施......有些东西是不正确的。
The thing is working, but I am not happy with the implementation... Some things are not right.
一个我遇到的问题是,如果我点击图像的右侧,看到未来的图片
的活动,它切换正常(位置++
),但旋转设备原来在位置
出现时。
One of the problems I am experiencing is that, if I click on the right of the image to see the next in the Photo
activity, it switches fine (position++
), but when rotating the device the original one at position
appears.
正在发生的事情是,图片
旋转图像,当被销毁由于某种原因再次重启,无需服从 super.onCreate(savedInstanceState )
,加载再额外(即位置
仅在图片
改变,而不是在父活动)。
What is happening is that Photo
is destroyed when rotating the image, and for some reason it restarts again, without obeying super.onCreate(savedInstanceState)
, loading again the Extras (the position
only changed in Photo
, not on the parent activities).
我试着用 startActivityForResult
而不是 startActivity
,但失败了......
I tried with startActivityForResult
instead of startActivity
, but failed...
当然,我可以做一些做作保存位置
数据,但应该有一些概念,我不理解如何活动的工作,我想这样做的权利。
Of course I can do something contrived to save the position
data, but there should be something "conceptual" that I am not understanding about how activities work, and I want to do this right.
有人可以解释我什么,我做错了,这是实现我想要的,而最好的方法,为什么?
Can someone please explain me what I am doing wrong, which is the best method to implement what I want, and why?
非常感谢!
推荐答案
试试这个code存储值的活动
Try this code to store the values for the activity
Long value;
protected void onSaveInstanceState(Bundle onOrientChange) {
super.onSaveInstanceState(onOrientChange);
onOrientChange.putLong("myValue", value);
}
和中的onCreate()恢复值:
And restore the values in onCreate():
public void onCreate(Bundle onOrientChange) {
if (onOrientChange!= null){
value = onOrientChange.getLong("myValue");
}
}
通常你恢复的onCreate您的状态()
。这是可能给它的 onRestoreInstanceState恢复()
为好,但不是很普遍。 ( onRestoreInstanceState()
在 ONSTART被称为()
,而的onCreate()
在 ONSTART被称为()
。
Usually you restore your state in onCreate()
. It is possible to restore it in onRestoreInstanceState()
as well, but not very common. (onRestoreInstanceState()
is called after onStart()
, whereas onCreate()
is called before onStart()
.
使用看跌期权的方法来存储值的onSaveInstanceState()
Use the put methods to store values in onSaveInstanceState()
这篇关于传递额外设备和屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!