Android--->activity高级运用,保存前一个界面为完成的数据savedInstanceState。
main.xml布局代码分析
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/txt" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="" /> <Button android:id="@+id/secondBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second" /> </LinearLayout>
second.xml布局文件代码分析
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SecondActivity" /> </LinearLayout>
main.xml所对应的操作代码MainActivity.java
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"); } //保存状态及数据到content 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() { 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); } }; }
second.xml布局文件所对应的操作SecondActivity.java
package com.szy.activity; import android.app.Activity; import android.os.Bundle; public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onPause() { super.onPause(); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); } @Override protected void onStart() { super.onStart(); } @Override protected void onStop() { super.onStop(); } }
activity布局界面需要在AndroidManifest.xml注册一下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.szy.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Wallpaper" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="@string/app_name"> </activity> <activity android:name=".ThirdActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog"> </activity> </application> </manifest>