You need to define that constant in MainActivity
as
public static final String EXTRA_MESSAGE = "extra_message";
so that in DisplayMessageActivity
you can access that static
constant as
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
MainActivity
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "extra_message";
// static : accessed by class name
// final : value cannot be changed
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayMessageActivity
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//..code
String message = getIntent().getStringExtra(MainActivity.EXTRA_MESSAGE);
//..code
}
}
2
solved Activity not reading intent from other class in Android SDK