This question has already been answered:

There is a first class in which there is a variable NameUser

There is a second class in which there is also a variable NameUser

From the first class, the value of the NameUser variable must be passed to the NameUser variable of the second class.

How to do?

First grade

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class HealthScreen extends AppCompatActivity{ TextView textView; String NameUser; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_health_screen); textView = (TextView) findViewById(R.id.textView3); MainActivity mainActivity = new MainActivity(); NameUser = mainActivity.NameUser; textView.setText(NameUser); } 

}

Second class

 public class MainActivity extends AppCompatActivity { TextView WelcomeAlpha; Button OK; Button OK1; ImageView line1; TextView FIO; TextView text_Weigth; EditText NameEdit; EditText WeigthEdit; Animation animation; Animation animation1; Animation animation2; String NameUser; String WeightUser; int timer = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); animation = AnimationUtils.loadAnimation(this, R.anim.fadein); animation1 = AnimationUtils.loadAnimation(this, R.anim.fadeout); animation2 = AnimationUtils.loadAnimation(this, R.anim.fadenow); line1 = (ImageView) findViewById(R.id.line); WelcomeAlpha = (TextView) findViewById(R.id.welcome_text_view); OK = (Button) findViewById(R.id.ok_button); OK1 = (Button) findViewById(R.id.ok_button_2); FIO = (TextView) findViewById(R.id.FIO); text_Weigth = (TextView) findViewById(R.id.textView2); NameEdit = (EditText) findViewById(R.id.editText); WeigthEdit = (EditText) findViewById(R.id.editText2); WelcomeAlpha.startAnimation(animation); line1.setVisibility(View.INVISIBLE); NameEdit.setVisibility(View.INVISIBLE); WeigthEdit.setVisibility(View.INVISIBLE); OK.setVisibility(View.INVISIBLE); OK1.setVisibility(View.INVISIBLE); FIO.startAnimation(animation2); text_Weigth.startAnimation(animation2); WeigthEdit.startAnimation(animation2); NameEdit.startAnimation(animation2); View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { WelcomeAlpha.setVisibility(View.INVISIBLE); OK.setVisibility(View.VISIBLE); NameEdit.setVisibility(View.VISIBLE); NameEdit.startAnimation(animation); OK.startAnimation(animation); FIO.startAnimation(animation); } }; WelcomeAlpha.setOnClickListener(onClickListener); View.OnClickListener onClickListener1 = new View.OnClickListener() { @Override public void onClick(View v) { NameUser = NameEdit.getText().toString(); line1.setVisibility(View.VISIBLE); line1.startAnimation(animation); OK1.setVisibility(View.VISIBLE); OK.setVisibility(View.INVISIBLE); WeigthEdit.setVisibility(View.VISIBLE); WeigthEdit.startAnimation(animation); OK1.startAnimation(animation); text_Weigth.startAnimation(animation); } }; OK.setOnClickListener(onClickListener1); View.OnClickListener onClickListener2 = new View.OnClickListener() { @Override public void onClick(View v) { WeightUser = WeigthEdit.getText().toString(); NameEdit.startAnimation(animation1); WeigthEdit.startAnimation(animation1); OK1.startAnimation(animation1); WeigthEdit.setVisibility(View.INVISIBLE); NameEdit.setVisibility(View.INVISIBLE); OK1.setVisibility(View.INVISIBLE); OK1.setClickable(false); NameEdit.setActivated(false); WeigthEdit.setActivated(false); line1.startAnimation(animation1); text_Weigth.startAnimation(animation1); FIO.startAnimation(animation1); GoToHealth(); } }; OK1.setOnClickListener(onClickListener2); } void GoToHealth() { Intent intent = new Intent(MainActivity.this, HealthScreen.class); startActivity(intent); this.finish(); } } 

Reported as a duplicate by pavlofff , zRrr , YuriSPb android 22 Jul '16 at 13:43

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • show the code. We must mean how these classes are interconnected - Vladyslav Matviienko
  • @metalurgus rules the question - Ker_ Jen
  • Pass it through Intent at the start of the second Activity. - Vladyslav Matviienko

1 answer 1

  1. Make a public method public string getUserName() , which will return userName ( return userName; ), and call it in another method:

     ClassWithVariable instance=new ClassWithVariable(); //создаете экземпляр класса String name = instance.getUserName(); //получаете переменную. 
  2. Make a public method in the class where you need to pass a variable that takes it as an argument:

     class MyClass { public void method(String userName); } class ClassWithVariable { private string userName = "Ivan"; MyClass instance = new MyClass(); instance.method(userName); } 
  • In your case, use a public property) - Valeriy Posvistak
  • 2
    @Ker_Jen In the case of activation (and in the question we are talking about activation), this solution is a crutch, and a problem one, since the existence of an invisible activation at the moment, and therefore its fields, is not guaranteed. The right decision is to transfer data via an Intent , interface or some kind of bus, like EventBus / - pavlofff