0

Assuming that we have two activities, FirstActivity.java and SecondActivity.java

In the first activity exists a String that changes value with user interaction. public String input;

When SecondActivity starts, it requires the value of the String to store it in a local String named "input" for its own use.

Greg
  • 357
  • 1
  • 11

3 Answers3

1

Two things come to mind in this situation :

  1. use SharedPreferences to save the value of your string for every change and then retrieve it from the second one whenever you need it.

  2. you could mark the String variable with public static and you'll be able to access it from anywhere in your code.

gemy845
  • 84
  • 6
  • I would not recommend using static variables, as they can lead to other problems down the road, especially if you get into multi-threading. It would likely be better to pass this information through the intent's bundle/extras. – Quinn Jul 12 '19 at 20:21
  • I agree, that's why I put it last. – gemy845 Jul 13 '19 at 11:14
  • shared preferences isn't necessarily ideal either, as it could be accessed in multiple spots at once and lead to similar issues. Also writing to a file for something as simple as passing an input seems unnecessary. It will work and do what OP wants, I just think in this case keeping it in memory and passing it through intents is cleaner. SP is only necessary if he is trying to keep the input string after the app is closed and reopened – Quinn Jul 13 '19 at 11:39
  • It's just preferences really, I for myself don't like to hassle with activity lifecycle methods and would prefer to override as few of them as possible to get the job done .. it's admittedly true that it turns out to be an overkill for some use cases -like this one- – gemy845 Jul 13 '19 at 11:49
1

I would not recommend using static variables or shared preferences for sharing information like this, although it will work, it could cause issues down the road and isn't necessarily good practice. A cleaner solution would be to pass the string in the intents bundle. You can take a look at this stack overflow question to get an idea of how to do that.

In your case, right before you switch to your second activity you can add the string to the intent like this:

intent.putExtra("input", input);

and in your second activities on create retrieve the string like this:

input = getIntent().getExtras().getString("input", "");

Quinn
  • 7,072
  • 7
  • 39
  • 61
0

Sharing data between fragments and activities is a very delicate matter. There's problems controlling the variables across lifecycle changes, propagating data changes, so on and so forth.. So, if you're starting a new app, or its a project still in the beginning, i strongly recommend you take a look at android Jetpack Architecture components, check the concepts of Single Activity architecture, Viewmodel, LiveData and Databinding. I really think thats the right way to do what you want. You will be fixed! Otherwise, @gemy845's answer looks just fine!

Thalis Vilela
  • 335
  • 1
  • 10