0

I am trying to send the value stored in a variable my_var from one activity to the other in Android. There are probably already many similar questions here at SOV, but I have been trying things by my own, and so far, no success. I shall highly appreciate little help or hints on what I am doing wrong?

My (pseudo/example) code is like this:

public class MyActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

public String my_var;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        
        My_method();

}

public void my_method() {

// This is a method that makes HTTP GET request and parse response to my_var

my_var = responseObject1.getX() + " " + responseObject1.getY()

}

// Then, at the bottom of MyActivity, I am creating an Intent to pass my_var to another activity to show it in TextView.
// I took this method from here[.][1]

   public void rsa_key(String s){
        Intent intent = new Intent(getBaseContext(), AnotherClass.class);
        intent.putExtra("my_var", my_var);
        startActivity(intent);

    }


}

Then, in the other activity (in OnCreate), I am trying to get my_var like this:

// public String my_var in initialization
Intent intent = getIntent();
my_var = intent.getStringExtra("my_var");

The app compiles, and I get no errors, but I can't see my_var value (XML layout) when put it in TextView.setText(my_var); in the other activity. There were no useful hints in the log as well. Can somebody help me to understand what am I doing wrong? or missing something.

I also tried SharedPreferences like this but no luck!

In first acitvity:

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("varKey", my_var);
    editor.commit();

Second actvity:

   SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
   my_var = sharedPref.getString("varKey", my_var);

I shall highly appreciate help/suggestions to fix this. Many thanks!

khajlk
  • 791
  • 1
  • 12
  • 32
  • Are you sure you are using the correct context? – Josip Domazet Dec 18 '21 at 16:13
  • I also used `this` and `FirstActivity.this`. Didn't work. What could possibly be wrong with the context? – khajlk Dec 18 '21 at 16:22
  • `public String my_var;` Change to `public String my_var="xxxxxcccc";` And try again. For the rest it looks ok. – blackapps Dec 18 '21 at 16:24
  • When I call `rsa_key` method, the page jumps to the other activity, which is not desired. I only want to pass the variable value while activity one is running (unless changed by the user). – khajlk Dec 18 '21 at 16:26
  • @blackapps: Tried and didn't help. Not sure, if i should call `rsa_key` because jumps to the next page (activity) in the first one. But passing my_var did not work. – khajlk Dec 18 '21 at 16:32
  • Show how and where you call `rsa_key()`. Why is that a public function? Makes no sense. – blackapps Dec 18 '21 at 17:06
  • `my_var = responseObject1.getX()` It unclear to me why you need all that code to show us your problem to transfer a variable to another activity. You could as well put there `my_var = "asssss";` – blackapps Dec 18 '21 at 17:10
  • `intent.putExtra("my_var", my_var);` Change to `my_var="ddddddddd"; intent.putExtra("my_var", my_var);`. – blackapps Dec 18 '21 at 17:11
  • how do you get the passed data in your second activity? – behrad Dec 18 '21 at 21:07
  • @blackapps: Tried your suggestion, didn't work! – khajlk Dec 19 '21 at 10:38
  • If you do not get to the point and give better answers how can we help you? What you tell makes no sense. You still did not show us where rsa_key() is called. And how. And why you made it a public function. – blackapps Dec 19 '21 at 10:39
  • @behrad: Isn't it already there in the question? I am doing `Intent intent = new getIntent();` Please check. – khajlk Dec 19 '21 at 10:39
  • you are doing the wrong code in getting data from the previous Activity. Please check this link and change your code to get the right way of getting extra data.https://stackoverflow.com/a/5265952/9474700 – behrad Dec 19 '21 at 20:01
  • @behrad: And how do I make it right? Care to post a comment or answer? – khajlk Dec 19 '21 at 20:03
  • You have "method that makes HTTP GET request" being called in onCreate? That should crash immediately for doing network call on the main thread. You also have `My_method()` and then `public void my_method()`, so that should not even compile. Please post a clear and complete example if you expect help. – dominicoder Dec 19 '21 at 22:24

1 Answers1

0

try this code for putExtra and get data in the second activity.

Use this to "put" the file...

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
i.putExtra("my_var", my_var);
startActivity(i);

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("my_var");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("my_var");
}
behrad
  • 1,228
  • 14
  • 21