2

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.

Toni_Entranced
  • 969
  • 2
  • 12
  • 29
  • Why don't you want to use `Intent`? This is exactly what it is for. Using `startActivityForResult()` in first `Activity` and `setResult()` in second `Activity`. – codeMagic Sep 03 '13 at 19:34
  • Depending on what you mean by " hierarchical parent", if second is a subclass of first then you can use a member variable in first then second should have access to it. – codeMagic Sep 03 '13 at 19:36
  • Second.class is not a subclass of First.class. Also, I didn't want to use Intent because I was worried that if I called startActivity, it would send the user back to the First.class layout, which I don't want. I'll take a look at startActivityForResult() and setResult() though, and get back to you. – Toni_Entranced Sep 03 '13 at 23:52
  • Ok, I really think `startActivityForResult()` will work for you but if not then consider `SharedPreferences` as Squonk has mentioned in that answer – codeMagic Sep 04 '13 at 00:14

1 Answers1

5

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

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Wasn't even thinking about `SharedPreferences` because `Intent` seems so obvious here...good point! – codeMagic Sep 04 '13 at 00:12
  • I think I'll be going with SharedPreferences. The reason being is, I'm actually trying to modify variables from an object, not just some primitive string. If I am to have First.class store an object which contains modifiable variables, how do I save them in SharedPreferences? SharedPreferences only stores primitives, doesn't it? – Toni_Entranced Sep 04 '13 at 00:30
  • I think I found the answer. It's a simple file and object i/o stream. I think I can try figuring out the rest from here. SharedPrefs was a very good thing for me to study though--I'll use it for primitive data should it come to it. :) – Toni_Entranced Sep 04 '13 at 04:02
  • @TonyN.Tran : If you need to do something more complex than `SharedPreferences` or passing a `Bundle` can cope with then you should consider using a helper class (standard Java POJO) which can hold anything you need and, if you design it as a singleton, there will only ever be one instance of it and it can be accessed by any of your application components. – Squonk Sep 04 '13 at 07:14
  • I see. Would you recommend doing that as opposed to setting up a tedious i/o stream? It sounds way easier than what I'm currently doing. – Toni_Entranced Sep 04 '13 at 14:55