如何修改一个活动的变量,从另一个活动? [英] How do I modify one activity's variables from another activity?

查看:267
本文介绍了如何修改一个活动的变量,从另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设
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 second Activity which need to be reflected in the first Activity must be either persisted using some form of global storage (SharedPreferences for example) or should be passed using the extras of an Intent or a Bundle.

With SharedPreferences simply have the first Activity save your currentValue before starting the second Activity and do the reverse in second Activity before returning to the first. The first Activity then simply needs to check the SharedPreferences in onResume() and update its TextView if necessary.

As codeMagic mentioned, however, simply using startActivityForResult(...) would allow passing currentValue from the first to the second Activity and, before the second exits, updating the Bundle with any changes will allow it to be passed back to the first Activity through onActivityResult(...).

这篇关于如何修改一个活动的变量,从另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆