[英]Android - ImageButton Loses Pressed State When New Activity Launched
I have an ImageButton
which changes depending on whether it is in a pressed state or not. 我有一个
ImageButton
,它会根据是否处于按下状态而变化。 This is working fine, until I launch a new activity. 在我启动一项新活动之前,它工作正常。
When an object from the ListView
is clicked it starts a new activity, but just before the new activity launches in to the foreground I can see the ImageButton
going back to it's default state. 单击
ListView
的对象时,它会启动一个新活动,但是就在新活动启动到前台之前,我可以看到ImageButton
返回其默认状态。
When I press the back button to go back to my MainActivity
, the ImageButton
is in it's default state but I want it to remain in the state that it is in when the ListView
is clicked to launch the new activity. 当我按下后退按钮返回到
MainActivity
, ImageButton
处于默认状态,但我希望它保持在单击ListView
启动新活动时的状态。
So in this scenario I would have wanted it to remain in it's pressed state through launching the new activity and then going back to the MainActivity
. 因此,在这种情况下,我希望通过启动新活动,然后返回
MainActivity
使其保持在受压状态。
I'm not sure if this is a default feature which just clears any sort of object states upon launching a new activity or if there is something I am doing incorrectly? 我不确定这是否是一项默认功能,它可以在启动新活动时清除所有对象状态,还是我做错了什么?
Also if this is default behaviour is there a way around it? 另外,如果这是默认行为,是否可以解决?
What I recomend to you is use the shared preferences to save the button state. 我建议您使用共享的首选项保存按钮状态。 You can see it in this post: how to save togglebutton state using shared preferences
您可以在这篇文章中看到它: 如何使用共享首选项保存切换按钮状态
The answer in the end was a very simple one. 最后的答案很简单。 In
onPause
, I stored the state of the ImageButton
in a Bundle
and then in onResume
I called the stored state from the Bundle
. 在
onPause
,我将ImageButton
的状态存储在Bundle
,然后在onResume
我从Bundle
调用了存储状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.