如何修改一个活动的变量,从另一个活动? [英] How do I modify one activity's variables from another activity?
问题描述
假设
First.class
具有可变
字符串CurrentValue的=红
一个按钮,导致Second.class(活动)。 First.class(活动)的一个TextView什么变量CurrentValue的恰好是显示器。 (目前,红色)。
如果我们按下按钮,它把我们带到Second.class,其中有一个EditText框修改变量First.class。它也有一个按钮来确认更改。最后,它在最底层一个TextView显示出First.class价值变量是什么preVIEW。
当在Second.class的EditText框蓝色用户类型和点击按钮,我们将如何改变从First.class不使用意图和打算回到那个活动的变量?我想留在Second.activity并从那里做的修改。
命中确认键后,preVIEW的TextView应该更新到新修改的变量相匹配。我们仍然应该看到Second.class,我提醒你。如果用户点击返回或上在这一点上,它们应返回First.class并且还看到,在First.class TextView的已被改变。
如何修改First.class'变量如果Second.class是First.class完全独立,不能访问它? (First.class是Second.class的分层父。
如何修改First.class'变量如果Second.class是First.class完全独立,不能访问它?
块引用>您不能或(更重要的),你不应该这样做。
Android的
活动
是一个特例级和一般应视为是自成一体。换句话说,第二活动
这就需要数据的任何修改在第一活动中反映出来
必须要么坚持使用某种形式的全球存储(共享preferences
)后还是应该使用的临时演员被传递的意图
或捆绑
。使用
共享preferences
只需第一个活动
保存CurrentValue的
启动第二活动
之前返回到第一前做相反的第二活动
。第一个活动
然后只需要检查共享preferences
在onResume()
并更新其的TextView
,如果必要的。由于codeMagic提及,但是,简单地使用
startActivityForResult(...)
将允许通过CurrentValue的
从第一到第二活动
和,第二个退出之前,更新捆绑
的任何更改将允许它被传递回第一个活动
到的onActivityResult(...)
。Let's say First.class has a variable String currentValue = "Red" with a button that leads to Second.class (an activity). First.class(Activity) displays in a textview what the variable currentValue happens to be. (Currently, Red).
If we push the button, it takes us to Second.class, which has an EditText box to modify the variable in First.class. It also has a button to confirm the change. Finally, it has a TextView at the very bottom showing a preview of what First.class' value variable is.
When a user types in "Blue" in Second.class' EditText box and hits the button, how would we change the variable from First.class without using intents and going back to that activity? I want to stay within Second.activity and do the changes from there.
After hitting the confirm button, the preview TextView should update to match the newly modified variable. We should still be seeing Second.class, I remind you. If the user hits "Back" or "up" at this point, they should return to First.class and also see that the TextView in First.class has been changed.
How do I modify First.class' variables if Second.class is entirely separate from First.class and cannot access it? (First.class is the hierarchical parent of Second.class.
解决方案How do I modify First.class' variables if Second.class is entirely separate from First.class and cannot access it?
You can't or (more importantly) you should not try to do this.
The Android
Activity
is a "special case" class and should be generally considered as being self-contained. In other words any changes to data in the secondActivity
which need to be reflected in the firstActivity
must be either persisted using some form of global storage (SharedPreferences
for example) or should be passed using the extras of anIntent
or aBundle
.With
SharedPreferences
simply have the firstActivity
save yourcurrentValue
before starting the secondActivity
and do the reverse in secondActivity
before returning to the first. The firstActivity
then simply needs to check theSharedPreferences
inonResume()
and update itsTextView
if necessary.As codeMagic mentioned, however, simply using
startActivityForResult(...)
would allow passingcurrentValue
from the first to the secondActivity
and, before the second exits, updating theBundle
with any changes will allow it to be passed back to the firstActivity
throughonActivityResult(...)
.这篇关于如何修改一个活动的变量,从另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!