關於部落格
  • 78070

    累積人氣

  • 2

    今日人氣

    0

    追蹤人氣

[Android] 比較onSaveInstanceState() 與 onRetainNonConfigurationInstance() 函式

(I) Example code by me
package com.test;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

public class test extends Activity {
    private static final String TAG = "test";
    ArrayList myLocation = new ArrayList();
    static int staticInt = 0;
    @Override
    protected void onCreate(Bundle state) {
      super.onCreate(state);
      Log.i(TAG, "onCreate()");
      setContentView(R.layout.main);
      //myLocation = new ArrayList();
    }
   
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume()");
          staticInt++;
          if(myLocation.size()==0){
              Toast.makeText(this, "No any location now, then create automatically ..", 3000).show();
              myLocation.add("Taipei "+staticInt);
              myLocation.add("Milan "+staticInt);
              myLocation.add("New York "+staticInt);
          }
         
          TextView myLocationTextView = (TextView) findViewById(R.id.my_Location);
          String myLocationString = "";
          if(myLocation.size()>0){
              for(int i=0;i<myLocation.size();i++) {
                  myLocationString += myLocation.get(i).toString() +"n";
              }
          }
          myLocationTextView.setText(myLocationString);
    }
   
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy()");
    }
   
   
    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "**** onSaveInstanceState ****");
        savedInstanceState.putIntegerArrayList("key_myLocation", myLocation);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "**** onRestoreInstance ****");
        if(savedInstanceState.get("key_myLocation")!= null) {
            myLocation = (ArrayList) savedInstanceState.get("key_myLocation");
            if(!myLocation.contains("HongKong"))
                myLocation.add("HongKong");
            Log.i(TAG, "**** onRestoreInstance: "+myLocation.size()+ " items ****");
        }
        else
             Log.i(TAG, "**** onRestoreInstance: No savedInstanceState !! ****");
    }
}


(II) Example code by me
package com.test;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

public class test extends Activity {
    private static final String TAG = "test";
    ArrayList myLocation = myLocation = new ArrayList();
    static int staticInt = 1;
    @Override
    protected void onCreate(Bundle state) {
      super.onCreate(state);
      Log.i(TAG, "onCreate()");
      setContentView(R.layout.main);
      getLastNonConfigurationInstance();

      if(getLastNonConfigurationInstance()!=null)
          myLocation = (ArrayList) getLastNonConfigurationInstance();
      else {
          myLocation.add("Taipei "+staticInt);
          myLocation.add("Milan "+staticInt);
          myLocation.add("New York "+staticInt);
      }
      staticInt++;
     
      TextView myLocationTextView = (TextView) findViewById(R.id.my_Location);
      String myLocationString = "";
      if(myLocation.size()>0){
          for(int i=0;i<myLocation.size();i++) {
              myLocationString += myLocation.get(i).toString() +"n";
          }
      }
      myLocationTextView.setText(myLocationString);
    }
   
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy()");
    }
   
    @Override
    public Object onRetainNonConfigurationInstance() {
        //Toast.makeText(this, "onRetainNonConfigurationInstance()", 3000).show();
        Log.i(TAG, "onRetainNonConfigurationInstance()");
        if(!myLocation.contains("Beijing"))
            myLocation.add("Beijing");
        return myLocation;
    }
}
相簿設定
標籤設定
相簿狀態