The answers given are here aren't wrong but they are incomplete in my opinion. The best way to do this with validations is to make sure that the extra is taken from the previous Activity as well as the savedInstanceState which is the Bundle data received while starting the Activity and can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.
Sending data -
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("name", "Daenerys Targaryen");
intent.putExtra("number", "69");
startActivity(intent);
Receiving data -
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
int no;
String na;
if(savedInstanceState == null){
Bundle extras = getIntent().getExtras();
if(extras != null){
no = Integer.parseInt(extras.getString("number"));
na = extras.getString("name");
}
}else{
no = (int) savedInstanceState.getSerializable("number");
na = (String) savedInstanceState.getSerializable("name");
}
// Other code
}