Transfer the variable from one activate to another
-
There's a first class in which there's a variable nameUser.
There's a second class in which there's also a variable nameUser.
From the first class, the nameUser change meaning should be transferred to the variable nameUser of the second class.
How do I do it?
First Class
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();
}
}
-
Make a public method
public string getUserName()
that will returnuserName
(sighs)return userName;
and call it in another method:ClassWithVariable instance=new ClassWithVariable(); //создаете экземпляр класса String name = instance.getUserName(); //получаете переменную.
Make a public method in a class where the variable is to be transferred, which accepts it as an argument:
class MyClass { public void method(String userName); }
class ClassWithVariable {
private string userName = "Ivan";MyClass instance = new MyClass(); instance.method(userName);
}