简体   繁体   中英

Android not restoring instance state

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.

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

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

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