Activity状态保存应用及Activity的主题皮肤学习
1.Activity状态的保存.
Activity生命周期函数事项介绍。
生命周期中的函数 函数的详细描述 是否能杀死进程程序 下个执行的函数方法
1.onCreate() 当activity被创建时调用。这是做一般的静态初始化的地方,比如创建界面, 否 onStart()
把数据绑定到列表,等等之类。这个方法会被传入一个 Bundle对像,它包含了
activity的上一次(关闭时)的状态(如果这个状态可以得到)。
此方法后面永远跟着onStart()。
2.onReStart() 在停止后被调用,但不是停止后马上调用,而是在再次开始前调用, 否 onStart()
也就是在再次调用onStart()之前立即调用。
3.onStart() 当activity变成可见后立即调用它。如果activity成为最上层, 否 onResume()或onStop()
则调用onResume(),如果完全被摭盖,就调用onStop()。
4.onResume() 当activity处于最上层时,立即调用此方法。 否 onPause()
此时activity获得输入焦点。后面跟着onPause()。
5.onPause() 当另一个activity要进入Pause状态时调用此方法。这个方法一般是 是 onResume()或onStop()
用来提交那些发生改变的永久化的数据们,停止动画和其它消耗CPU的玩意们。
这些工作必须以最快的速度完成,因为在这个方法返回之前,
另一个activity就不能进入resume状态。当它又回到最上层时,后面跟着onResume(),
当它被完全摭盖时,后面跟着onStop()。
6.onStop() 当activity被完全摭盖时被调用。当activity要销毁时或被其它activity完全 是 onRestart()或onDestory()
摭盖时都会发生。如果这个activity又回到最上层,则后面跟着onRestart(),
如果它逝去了,则跟着onDestroy()。
7.onDestory() 在activity销毁之前被调用。这是activity能收到的最后一个调用。 是 进程关闭程序完全销毁
调用的原因可能是别人在这个activity上调用了finish(),也可能是系统为了更多的
内存空间而把它所在的进程处死了。在这个方法中,
可以调用isFinishing()来判断自己属于哪一种死法。
如果我们在进行操作这时 有其他的动作要执行,这时程序就会处于 onStop()状态,由于设备的内存资源消耗的太大,Android操作系统就会杀死一些进程,
来释放资源,这时我们就要在杀死之前保存上次操作时的数据和状态。
注意:要保存数据要覆盖 : protected void onSaveInstanceState(Bundle outState) 在此方法中进行运行程序状态和数据的保存。
直接上显示源码
前台布局源码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText android:id="@+id/txt_account"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"/>
<EditText android:id="@+id/txt_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/>
<Button android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"/>
</LinearLayout>
</LinearLayout>
后台源码:
public class MainActivity extends Activity
{
private final static String TAG="MainActivity";
private final static String CONFIG="Account";
private EditText txt_account=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "MainActivity==onCreate");
txt_account=(EditText)findViewById(R.id.txt_account);
//判断获取保存的数据
if(null!=savedInstanceState && savedInstanceState.containsKey(CONFIG))
{
String account=savedInstanceState.getString(CONFIG);
txt_account.setText(account);
}
Button btn_login=(Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(listener);
Button btn_exit=(Button)findViewById(R.id.btn_exit);
btn_exit.setOnClickListener(listener);
}
//保存状态覆盖此方法
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
Log.i(TAG, "MainActivity==onSaveInstanceState");
//保存数据
outState.putString(CONFIG,txt_account.getText().toString());
}
private OnClickListener listener=new OnClickListener()
{
@Override
public void onClick(View view)
{
Button btn=(Button)view;
switch (btn.getId())
{
case R.id.btn_login:
{
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
MainActivity.this.startActivity(intent);
}
break;
case R.id.btn_exit:
finish();
break;
default:
break;
}
}
};
@Override
protected void onStart()
{
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG, "MainActivity==onStart");
}
@Override
protected void onRestart()
{
// TODO Auto-generated method stub
super.onRestart();
Log.i(TAG, "MainActivity==onRestart");
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Log.i(TAG, "MainActivity==onResume");
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG, "MainActivity==onPause");
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
Log.i(TAG, "MainActivity==onStop");
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG, "MainActivity==onDestroy");
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
效果图:
运行LogCat日志信息。此时我们点击登录到第二个Activity时 。就会调用 onSaveInstanceState()方法进行保存。
:
由于本人用模拟器无法演示:程序在OnStop()时被Android系统杀死。如果在后台被系统杀死前,就会调用onSaveInstanceState()方法进行保存。
如果此时在启动应用程序 在onCreare()方法中 判断 Bundle 的值 ,取出临时保存的程序的运行状态就数据。
2.Activity的主题设置。
android:theme
1. android:theme="@android:style/Theme.Dialog"
2. android:theme="@android:style/Theme.NoTitleBar"
3. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
4. android:theme="Theme.Light" android:theme="Theme.Light.NoTitleBar"
5. android:theme="Theme.Light.NoTitleBar.Fullscreen"
6. android:theme="Theme.Black" android:theme="Theme.Black.NoTitleBar"
7. android:theme="Theme.Black.NoTitleBar.Fullscreen"
8.android:theme="Theme.Wallpaper"
9.android:theme="Theme.Wallpaper.NoTitleBar"
10. android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"
11.android:theme="Translucent"
12. android:theme="Theme.Translucent.NoTitleBar"
13.android:theme="Theme.Translucent.NoTitleBar.Fullscreen"
14.android:theme="Theme.Panel" android:theme="Theme.Light.Panel"
简单对主题进行截图介绍:我们对SecondActivity 运行主题Theme
SencondActivity前台布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"/>
</LinearLayout>
后台源码
public class SecondActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btn_close=(Button)findViewById(R.id.btn_close);
btn_close.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
finish();
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_second, menu);
return true;
}
}
没有应用Theme主题是 截图
在配置文件中添加主题:
<activity
android:name="com.example.activitysavestatedemo.SecondActivity"
android:label="@string/title_activity_second"
android:theme="@android:style/Theme.Dialog" >
</activity>
1.android:theme="@android:style/Theme.Dialog" 效果截图: 弹出一个模式对话框,此时只能操作对话框。
2.android:theme="@android:style/Theme.NoTitleBar" 效果截图 :背景变黑色 去掉了标题栏。
3.android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 效果图:背景黑色 连时间 和 电池电量的也消失了。程序占满整个屏幕。
4.android:theme="@android:style/Theme.Light" 效果图:和默认没有什么区别。
5.android:theme="@android:style/Theme.Light.NoTitleBar" 效果图:标题栏消失了。
6.android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 效果图:全屏 占据整个设备屏幕。
7.android:theme="@android:style/Theme.Black" 和 4 的截图一样 只是背景为 黑色(Black)
8.android:theme="@android:style/Theme.Black.NoTitleBar" 和5 的截图一样 只是背景为 黑色(Black)
9.android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 和6 的截图一样 只是背景为 黑色(Black)
10.android:theme="@android:style/Theme.Wallpaper" 效果图:和4 的截图一样 只是背景为 设备的壁纸。
11.android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 效果图:和5 的截图一样 只是背景为 设备的壁纸。
12.android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 效果图:和6 的截图一样 只是背景为 设备的壁纸。
13.android:theme="@android:style/Translucent" 截图为: 和 4 的截图一样 只是背景为 半透明
14.android:theme="@android:style/Theme.Translucent.NoTitleBar"效果图:和5 的截图一样 只是背景为 半透明。
15.android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 效果图:和6 的截图一样 只是背景为 半透明。
16.android:theme="@android:style/Theme.Panel" 效果图:很像Theme.Dialog 但是不同是 背景颜色 还有 没有标题栏。一个Panel容器。
17.android:theme="@android:style/Theme.Light.Panel" 效果图:和Them.Panel没有发现有什么特别之处。
18.android:theme="@android:style/Theme.InputMethod" 效果图: 和 16 和 17没有发现什么区别。
19..android:theme="@android:style/Theme.NoDisplay" 效果图: 点击时Activity并没有显示出来。
20..android:theme="@android:style/Theme.WallpaperSettings" 效果图 :有如 模式对话框。并半透明。
本文写到这里。写的很啰嗦。欢迎同行们指出不足之处。