`
wang_peng1
  • 浏览: 3910865 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

两种保存方法

UI 
阅读更多

暂时的

@Override
public void onSaveInstanceState(Bundle savedInstanceState
{
  // Store UI state to the savedInstanceState.
  // This bundle will be passed to onCreate on next call.

  EditText txtName = (EditText)findViewById(R.id.txtName);
  String strName = txtName.getText().toString();
  
  EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
  String strEmail = txtEmail.getText().toString();
  
  CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
  boolean blnTandC = chkTandC.isChecked();
  
  savedInstanceState.putString("Name", strName);
  savedInstanceState.putString("Email", strEmail);
  savedInstanceState.putBoolean("TandC", blnTandC);
      
  super.onSaveInstanceState(savedInstanceState);
}

灰度

恢复

@Override
public void onCreate(Bundle savedInstanceState
{
    super.onCreate(savedInstanceState);
    
  // Restore UI state from the savedInstanceState.
  if (savedInstanceState != null)
  {
    String strValue = savedInstanceState.getString("Name");
    if (strValue != null)
    {
      EditText oControl = (EditText)findViewById(R.id.txtName);
      oControl.setText(strValue);
    }
    
    strValue = savedInstanceState.getString("Email");
    if (strValue != null)
    {
      EditText oControl = (EditText)findViewById(R.id.txtEmail);
      oControl.setText(strValue);
    }
    
    CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
    chkTandC.setChecked(savedInstanceState.getBoolean("TandC"));
  }
}

持续的

@Override
protected void onPause() 
{
  super.onPause();
  
  // Store values between instances here
  SharedPreferences preferences = getPreferences(MODE_PRIVATE);
  SharedPreferences.Editor editor = preferences.edit();

  // Put the values from the UI
  EditText txtName = (EditText)findViewById(R.id.txtName);
  String strName = txtName.getText().toString();
  
  EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
  String strEmail = txtEmail.getText().toString();
  
  CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
  boolean blnTandC = chkTandC.isChecked();
  
  editor.putString("Name", strName)// value to store
  editor.putString("Email", strEmail)// value to store
  editor.putBoolean("TandC", blnTandC)// value to store    
  // Commit to storage
  editor.commit();
}/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState
{
  super.onCreate(savedInstanceState);
  
  // Get the between instance stored values
  SharedPreferences preferences = getPreferences(MODE_PRIVATE);
  
  // Set the values of the UI
  EditText oControl = (EditText)findViewById(R.id.txtName);
  oControl.setText(preferences.getString("Name"null));
  
  oControl = (EditText)findViewById(R.id.txtEmail);
  oControl.setText(preferences.getString("Email"null));
  
  CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
  chkTandC.setChecked(preferences.getBoolean("TandC"false));
}

前一种主要是在旋转凭 以及登录信息,也就是说app关闭则保存状态小时,activity切换没有关系不影响

后一种关掉重启 继续保留

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics