This question has already been answered:
- Data transfer between Activity 2 replies
- Save and Load Activity 1 Response Status
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(); } }