I am developing a new app for practical on Android fundamentals 02.2:
Android fundamentals 02.2: Activity lifecycle and state
please slide to the bottom of the webpage, where is the project that the problem occur.
my problem is in the title, and the following is supplement: whether clicking back button of Second Activity to MainActivity or rotating the screen in MainActivity, the MainActivity will be executing the onSaveInstanceState method and be destroyed, then recreate.But in the case of the former, the argument of onCreate savedInstanceState is null while being recreated, in the case of the latter, the argument is not null,which is normal and it can display the data stored in Bundle again.What caused the result?
Please let me state the procedures that make the problem recur:
- run the app
- enter something in the editText, then click SEND
- enter something in the editText of Second Activity, then click REPLY
- you can see the content you enter in the Second Activity displays at the top of MainActivity
- continue to enter something and click the SEND to launch Second Activity
- now click the back button on the top left of the SecondActivity, instead of clicking the REPLY
Now you can see the content displays at the top of MainActivity has disappeared.But when I checked the log, I saw that the MainActivity had indeed executed the onSaveInstanceState before it destroyed(I added a log statement in that method).But when the MainActivity recreate, the argument in the OnCreate method savedInstanceState is null and there is nothing on the screen.Where is the data?
On the contrary, when I rotate the screen, the problem doesn't occur and the content displays at the top of MainActivity is still there(rotating the screen belongs to system configuration change, the Actitvity will also destroy firstly and then recreate).It means that the savedInstanceState is not null and the app run normally What and why lead into this problem, I don't understand, please help me. Thanks a lot!
MainActivity:
package com.example.twoactivities;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG =
MainActivity.class.getSimpleName();
public static final String EXTRA_MESSAGE =
"com.example.android.twoactivities.extra.MESSAGE";
public static final int TEXT_REQUEST = 1;
private TextView mReplyHeadTextView;
private TextView mReplyTextView;
private EditText mMessageEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(LOG_TAG, "-------");
Log.d(LOG_TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageEditText = findViewById(R.id.editText_main);
mReplyHeadTextView = findViewById(R.id.text_header_reply);
mReplyTextView = findViewById(R.id.text_message_reply);
if(savedInstanceState != null){
Log.d(LOG_TAG, "enter saveInstance");
boolean isVisible = savedInstanceState.getBoolean("reply_visible");
if(isVisible){
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(savedInstanceState.getString("reply_text"));
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button clicked!");
String message = mMessageEditText.getText().toString();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivityForResult(intent, TEXT_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == TEXT_REQUEST){
if(resultCode == RESULT_OK){
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onStart(){
super.onStart();
Log.d(LOG_TAG, "onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.d(LOG_TAG, "onPause");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(LOG_TAG, "onRestart");
}
@Override
protected void onStop() {
super.onStop();
Log.d(LOG_TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "onDestroy");
}
@Override
protected void onResume() {
super.onResume();
Log.d(LOG_TAG, "onResume");
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(LOG_TAG, "saving Instance");
if(mReplyHeadTextView.getVisibility() == View.VISIBLE){
outState.putBoolean("reply_visible", true);
outState.putString("reply_text", mReplyTextView.getText().toString());
}
}
}
SecondActivity:
package com.example.twoactivities;
public class SecondActivity extends AppCompatActivity {
public static final String LOG_TAG =
SecondActivity.class.getSimpleName();
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
private EditText mReply;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(LOG_TAG, "-------");
Log.d(LOG_TAG, "Sec onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mReply = findViewById(R.id.editText_second);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message);
textView.setText(message);
}
public void returnReply(View view) {
String reply = mReply.getText().toString();
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
finish();
Log.d(LOG_TAG, "End SecondActivity");
}
@Override
public void onStart(){
super.onStart();
Log.d(LOG_TAG, "Sec onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.d(LOG_TAG, "Sec onPause");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(LOG_TAG, "Sec onRestart");
}
@Override
protected void onStop() {
super.onStop();
Log.d(LOG_TAG,"Sec onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "Sec onDestroy");
}
@Override
protected void onResume() {
super.onResume();
Log.d(LOG_TAG, "Sec onResume");
}
}