1

I know similar questions have been asked but none of the solutions to those questions have been working for me.

When I go ahead and try to save the state of my app, the state of the EditText views are not being saved and restored. I went ahead and commented everything out and just put in a temporary string to save but when the app loads up again, the onCreate() method does not print 'Restoring instance state'

package com.fwumdesoft.udppacketsender;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
 * Posts a UDP message with the given data to the
 * target address on the target port.
 */
class UdpPostActivity extends AppCompatActivity {
    private static final String TAG = "UdpPostActivity";

    private static final String stateTextTargetHost = "com.fwumdesoft#TargetHost";
    private static final String stateTextTargetPort = "com.fwumdesoft#TargetPort";
    private static final String stateTextHexData = "com.fwumdesoft#HexData";
    private static final String stateTextStringData = "com.fwumdesoft#StringData";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_udp_post);

        if (savedInstanceState != null) {
//            ((EditText) findViewById(R.id.txtAddress)).setText(savedInstanceState.getString(stateTextTargetHost));
//            ((EditText) findViewById(R.id.txtPort)).setText(savedInstanceState.getString(stateTextTargetPort));
//            ((EditText) findViewById(R.id.txtData)).setText(savedInstanceState.getString(stateTextHexData));
//            ((EditText) findViewById(R.id.txtStringData)).setText(savedInstanceState.getString(stateTextStringData));
            String text = savedInstanceState.getString(stateTextStringData);
            Log.v(TAG, "Restoring instance state");
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        outState.putString(stateTextTargetHost, ((EditText)findViewById(R.id.txtAddress)).getText().toString());
//        outState.putString(stateTextTargetPort, ((EditText)findViewById(R.id.txtPort)).getText().toString());
//        outState.putString(stateTextHexData, ((EditText)findViewById(R.id.txtData)).getText().toString());
//        outState.putString(stateTextStringData, ((EditText)findViewById(R.id.txtStringData)).getText().toString());
        super.onSaveInstanceState(outState);
        outState.putString(stateTextStringData, "test");
        Log.v(TAG, "Saved instance state");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.v(TAG, "onRestore");
//        ((EditText)findViewById(R.id.txtAddress)).setText(savedInstanceState.getString(stateTextTargetHost));
//        ((EditText)findViewById(R.id.txtPort)).setText(savedInstanceState.getString(stateTextTargetPort));
//        ((EditText)findViewById(R.id.txtData)).setText(savedInstanceState.getString(stateTextHexData));
//        ((EditText)findViewById(R.id.txtStringData)).setText(savedInstanceState.getString(stateTextStringData));

    }

In the logcat output I get "Saved instance state" and then "onCreate" but I do not get "Restoring instance state" or "onRestore" when restarting the app.

jcarrete5
  • 37
  • 2
  • 7

2 Answers2

0

You should refer to the key you gave in outState when checking if savedInstanceState is not null, like this.

            if (savedInstanceState != null) {
            String text = savedInstanceState.getString("test");
            Log.v(TAG, "Restoring instance state");
        }

This documentation may shed some light

https://developer.android.com/guide/components/activities/activity-lifecycle.html

wnieves19
  • 550
  • 4
  • 16
  • Thank you for your answer. I have looked at the android lifecycle before attempting this but I cannot understand why the instance state isn't being restored when I gave it a value to store in the Bundle when the onSaveInstanceState method was invoked. I fixed the problem with reading the key from the Bundle. – jcarrete5 Apr 14 '17 at 17:02
  • I think this post: http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate has a good explanation. You should restore your state in the onCreate method by using the Bundle object savedInstanceState. Now you can use your text variable to set the text in any of the EditText. Please let me know if it worked ? – wnieves19 Apr 14 '17 at 17:07
  • I am trying to restore the state from the Bundle passed to the onCreate method but the Bundle passed to the onCreate method is null. I don't understand why the Bundle would be null when I had previously set a value in the Bundle in the onSaveInstanceState method. What determines if the Bundle is null? I am restarting the activity multiple times but the Bundle remains null. – jcarrete5 Apr 14 '17 at 17:18
  • The Bundle might be null for multiple reasons, first make sure that your stateTextStringData variable has a value in the onSaveInstanceState method. Second check your activity the AndroidManifest and make sure this attribute is not set to true android:clearTaskOnLaunch="true" – wnieves19 Apr 14 '17 at 17:28
0

Go to debug mode and see if your onCreate is being called. Do you have android:configChanges set in your manifest?

Kia
  • 124
  • 1
  • 1
  • 10
  • When I enter debug mode, "onCreate" does get printed to the logcat output. In my manifest, I have ' android:configChanges="orientation" ' as an attribute for the activity. – jcarrete5 Apr 14 '17 at 17:09
  • That means on an orientation change, onCreate will not get called since you specify that you will handle the changes in layout yourself and there is no need to reset the view. Your onCreate gets only called on the activity start, so it wouldn't get called on an orientation change. That's why you don't see "Restoring instance state." – Kia Apr 14 '17 at 17:14
  • The onCreate method might not get called on orientation change but that is not what my problem is. The onCreate method _is_ getting called when I reopen the app since my log message `Log.v(TAG, "onCreate");` is being printed when I reopen the app. I'm not sure why the Bundle savedInstanceState is null after I reopen the app when I have saved information in the Bundle in the onSaveInstanceState method – jcarrete5 Apr 14 '17 at 17:24
  • Data saved in bundle are not permanent. When you reopen the app it becomes null again. They only contain the data on a configuration change such as changing the orientation, language, or text size of the phone. If you want to save those data and have been stay there even after closing and reopening the app you need to consider SharedPreferences. https://developer.android.com/reference/android/content/SharedPreferences.html – Kia Apr 14 '17 at 17:27
  • Ok that makes sense. Thank you. – jcarrete5 Apr 14 '17 at 17:29
  • Although I am still unsure as to why my EditText views are not retaining the text stored in them. The android documentation states: "By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText widget). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you." [link](https://developer.android.com/guide/components/activities/activity-lifecycle.html) – jcarrete5 Apr 14 '17 at 17:35
  • Yep, if there is a text in an EditText and you rotate, it will be there after the activity is destroyed and recreated without any additional code from you. I suggest you create an EditText and simply set it up in onCreate and run and the app and experiment from there. I'm thinking you other changes on "onRestoreInstanceState" might be overriding those. – Kia Apr 14 '17 at 17:43