0

I am learning how to use Android Studio and I'm trying to save my activity's state. Something is wrong with the onSaveInstanceState method because when I run the code the savedInstanceState is always null. What can I do fix the problem? I've been trying for hours and I just don't see what's wrong. Here's the relevant stuff of mainActivity file.

public class MainActivity extends AppCompatActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    static final String LAST_MESSAGE = "com.example.myfirstapp.LAST_MESSAGE";
    String message, displayMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        System.out.println("Restoring");
        System.out.println(savedInstanceState);
        if (savedInstanceState != null) {
            displayMessage = "Last Saved Message: " + savedInstanceState.getString(LAST_MESSAGE);
        }
        else {
            displayMessage = "No Messages Saved!";
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, displayMessage, Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString(LAST_MESSAGE, message);

        super.onSaveInstanceState(savedInstanceState);
    }

    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • How are you expecting there to be a `savedInstanceState`? When you `startActivity` with that Activity, it won't be saved. Have you tried rotating the screen? – OneCricketeer Jul 13 '16 at 03:21
  • You can refer to [this diagram](https://developer.android.com/images/fundamentals/restore_instance.png) for how the `onSaveInstanceState` method is triggered. – OneCricketeer Jul 13 '16 at 03:24
  • In your code, data only save when rotating the screen. If you want save simple data you can use Sharedpreference or if you store large data you can save with database (Ex: SQLite, Realm...). – Iris Louis Jul 13 '16 at 03:40
  • Thanks, I've rotated the screen and `savedInstanceState` isn't null anymore but another problem seems to have arisen: I've added a `println` statement to check when the method is called. It seems to be called when the activity is changed as well as when the screen is rotated, but `savedInstanceState` is only changed when the screen is rotated. How is this? – TroubleMaker Jul 13 '16 at 12:16

1 Answers1

0

Firstly, you have to understand Activity Lifecycle. https://developer.android.com/guide/components/activities.html

Also read this. When are onSaveInstanceState() and onRestoreInstanceState() called, exactly?

In short, savedInstanceState should be null when activity executed in first time. When you come back to MainActivity from any page, you may get something from onRestoreInstanceState.

Community
  • 1
  • 1
Chan Chun Him
  • 791
  • 8
  • 12