I have implemented onPause() and onResume() method in my application as below:
protected void onPause() {
super.onPause();
String receiver = phoneNoField.getText().toString();
String message = messageBody.getText().toString();
getIntent().putExtra(MESSAGE_RECEIVER, receiver);
getIntent().putExtra(MESSAGE_BODY, message);
Log.d(TAG, receiver + " " + message);
}
protected void onResume() {
super.onResume();
String receiver = getIntent().getStringExtra(MESSAGE_RECEIVER);
String message = getIntent().getStringExtra(MESSAGE_BODY);
if(receiver != null)
phoneNoField.setText(receiver);
if(message != null)
messageBody.setText(message);
Log.d(TAG, receiver + " " + message);
}
When onPause() method is called, i see the values have been set. But in my onResume() method getStringExtra() always returns null. Anything wrong with my approach?
getIntent()returns theIntentthat has started the activity. When you go to another activity and then come back, whatgetIntent()returns is different from what you have had in theonPausemethodOne of your options is to put the values in the
Intentthat you use to start activityBand then when you start back activityAyet again to put the values in theIntent. The other option, which I’d prefer is to use SharedPreferences to do the job.