2017-04-14 56 views
0

我知道類似的問題已經被問到,但這些問題的解決方案都沒有爲我工作。Android不恢復實例狀態

當我繼續嘗試保存我的應用程序的狀態時,EditText視圖的狀態不會被保存和恢復。我繼續和評論的一切了,只是放在一個臨時字符串來保存,但是當應用程序加載再升,在onCreate()方法不打印「恢復實例狀態」

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)); 

    } 

在logcat的輸出我得到「保存的實例狀態」,然後「onCreate」,但我沒有在重新啓動應用程序時得到「恢復實例狀態」或「onRestore」。

回答

0

進入調試模式,看看是否被調用您的onCreate。你的清單中是否設置了android:configChanges?

+0

當我進入調試模式時,「onCreate」確實打印到logcat輸出。在我的清單中,我有'android:configChanges ='orientation''作爲活動的屬性。 – jcarrete5

+0

這意味着在方向更改時,onCreate不會被調用,因爲您指定要自己處理佈局中的更改,並且不需要重置視圖。您的onCreate只會在活動開始時被調用,所以它不會在方向更改時調用。這就是爲什麼你沒有看到「恢復實例狀態」。 – Kia

+0

onCreate方法可能不會調用方向更改,但這不是我的問題所在。自從我的日誌消息Log.v(TAG,「onCreate」)以來,當我重新打開該應用程序時,onCreate方法會被調用;當我重新打開該應用程序時,正在打印該文件。我不確定爲什麼在我將onSaveInstanceState方法的Bundle中的信息保存到應用程序後重新打開應用程序時Bundle savedInstanceState爲null – jcarrete5

0

當檢查savedInstanceState是否爲null時,應參考您在outState中輸入的鍵。

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

這個文件也可以提供一些線索

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

+0

謝謝你的回答。在嘗試此操作之前,我已經查看了android生命週期,但我無法理解爲什麼在調用onSaveInstanceState方法時,我給它一個存儲在Bundle中的值時,實例狀態不會恢復。我通過閱讀Bundle中的密鑰解決了這個問題。 – jcarrete5

+0

我覺得這個職位: http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate 有一個很好的解釋。您應該使用Bundle對象savedInstanceState在onCreate方法中恢復狀態。現在您可以使用您的文本變量來設置任何EditText中的文本。請讓我知道它是否有效? – wnieves19

+0

我想從傳遞給onCreate方法的Bundle中恢復狀態,但傳遞給onCreate方法的Bundle爲null。我不明白爲什麼在我之前在onSaveInstanceState方法中的Bundle中設置一個值時,Bundle將爲空。什麼決定了Bundle是否爲空?我正在多次重新啓動該活動,但該包仍然爲空。 – jcarrete5