首页 > 代码库 > 06 bundle 对象
06 bundle 对象
package com.szy.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private final static String TAG="MainActivity"; private static final String CONTENT = "content"; private Button secondBtn=null; private EditText txt = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt=(EditText)findViewById(R.id.txt); if (null!=savedInstanceState&&savedInstanceState.containsKey(CONTENT)) { txt.setText(savedInstanceState.getString(CONTENT)); } secondBtn=(Button)findViewById(R.id.secondBtn); secondBtn.setOnClickListener(listener); Log.i(TAG, "MainActivity-->onCreate"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "MainActivity-->onDestroy"); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "MainActivity-->onPause"); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG, "MainActivity-->onRestart"); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "MainActivity-->onResume"); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "MainActivity-->onStart"); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "MainActivity-->onStop"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.i(TAG, "MainActivity-->onSaveInstanceState"); String content=txt.getText().toString(); outState.putString(CONTENT, content); } private OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { Button btn=(Button)v; Intent intent=new Intent(); switch (btn.getId()) { case R.id.secondBtn: intent.setClass(MainActivity.this, SecondActivity.class); break; } startActivity(intent); } }; }
protected void onSaveInstanceState(Bundle outState) 中的bundle对象由系统保存,下次执行时候 会将bundle传进来。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。