Ricardo Ricardo - 1 month ago 4
Android Question

Android Studio, Using a variable to another Activiy

public class MainActivity extends AppCompatActivity {
EditText nama;
Button OK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerButton_OK();
}

private void addListenerButton_OK() {
nama=(EditText)findViewById(R.id.editText_NamaAnda);
OK=(Button)findViewById(R.id.button_OK);
OK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(nama.getText().toString().equals("Fredy")||nama.getText().toString().equals("Oktafianus")||nama.getText().toString().equals("Markus")
||nama.getText().toString().equals("fredy")||nama.getText().toString().equals("oktafianus")||nama.getText().toString().equals("markus")){
Intent Clicked_OK=new Intent(MainActivity.this,Game.class);

startActivity(Clicked_OK);
}else{

Toast.makeText(getBaseContext(),"Error : "+nama.getText()+" Bukan nama depan anda!",Toast.LENGTH_LONG).show();
}
}


});
}


}

I need help here!
I created MainActiviy and second Activty called Game.
I want to sending Edit Text nama; to second Activity.
I want to use "nama" in my second activity.

thanks before!

Answer

Try this,

Intent Clicked_OK = new Intent(MainActivity.this, Game.class);
Clicked_OK.putExtra("nama",nama.getText().toString());
startActivity(Clicked_OK);

On Game class onCreate method get "nama" from this way,

String nama =  getIntent().getExtras().getString("nama");

An Intent is a messaging object you can use to request an action from another app component.

Extras Key-value pairs that carry additional information required to accomplish the requested action.