简体   繁体   中英

Bundle savedInstanceState is null on onCreate() although savedInstanceState is already set data

I have a problem when getting state of activity. I have 2 activities: MainActivity and Activity2. In MainActivity I have put an editText and a button name GO. In Activity2, I have a button name BackMainActivity . What I want is that: I put a text, for example:"abc" into EditText then click button GO. App will navigate to Activity2. After that, I click button BackMainActivity, app will navigate to MainActivity and data in EditText is restore as "abc". I already used onSaveInstanceState and onRestoreInstanceState. App run through onSaveInstanceState before going to Activity2. But if I go back to MainActivity, onCreate(), savedInstanceState is null. Can you show me the reason ? I want to store data of mainActivity in bundle. So how can I do ? Thank you very much !

MainActivity

package com.example.demosavedataactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText t;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t = (EditText) findViewById(R.id.editText1);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this, Activity2.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        String a =  t.getText().toString();
        outState.putString("text",a);

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        t.setText(savedInstanceState.getString("text"));
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        Log.w("<<<<<<<<<<<<<<<<<<<", "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        Log.w("<<<<<<<<<<<<<<<<<<<", "onPause");
        super.onPause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        Log.w("<<<<<<<<<<<<<<<<<<<", "onRestart");
        super.onRestart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        Log.w("<<<<<<<<<<<<<<<<<<<", "onResume");
        super.onResume();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        Log.w("<<<<<<<<<<<<<<<<<<<", "onStart");
        super.onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        Log.w("<<<<<<<<<<<<<<<<<<<", "onStop");
        super.onStop();
    }
}

Activity2

package com.example.demosavedataactivity;    
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Activity2 extends Activity {

    Button back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        back = (Button) findViewById(R.id.button1);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Activity2.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

}

activity2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CombackA1" />

</LinearLayout>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="18dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="63dp"
        android:text="Button" />

</RelativeLayout>

Just do something like:

1: Fetch data from onSavedInstanceState in your oncreate() and put it in your edittext . Something like:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t = (EditText) findViewById(R.id.editText1);
    b = (Button) findViewById(R.id.button1);

    /*Just fetch data from savedInstanceState */
    if(savedInstanceState != null){
         t.setText(savedInstanceState.getString("text"));
    }
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Activity2.class);              
            startActivity(intent);
        }
    });
  }

And in your Activity2 set a FLAG_ACTIVITY_REORDER_TO_FRONT flag with your intent.like:

 Intent intent = new Intent(Activity2.this, MainActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
 startActivity(intent);

For a better idea just check this conversation .

In the onClick function of your Activity2 you don't go back to the previous MainActivity but you open a new MainActivity.

Just finish Activity2 by calling finish() then your first MainActivity will come up again. It should be working with the hardware back button of your device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM