展会信息港展会大全

Android学习笔记---Activity状态保存及Activity主题皮肤学习
来源:互联网   发布日期:2016-01-14 10:28:48   浏览:838次  

导读:Activity状态保存应用及Activity的主题皮肤学习1.Activity状态的保存. Activity生命周期函数事项介绍。生命周期中的函数 ...

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"效果截图:弹出一个模式对话框,此时只能操作对话框。

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港