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>
Android--->activity高级运用,保存前一个界面为完成的数据savedInstanceState。的更多相关文章
- Android Activity中状态保存机制
在Activity中保存用户的当前操作状态,如输入框中的文本,一般情况下载按了home键后,重新进入文本框中的东西会丢下,所以我们要保存当前页面信息,如在写短信的时候接到一个电话,那么当你接电话的时候 ...
- android activity状态的保存
今天接到一个电面,途中面试官问到一个问题,如果一个activity在后台的时候,因为内存不足可能被杀死,在这之前如果想保存其中的状态数据,比如说客户填的一些信息之类的,该在哪个方法中进行. onSav ...
- 【Android4高级编程笔记】深入探讨Android Activity
创建Activity 要创建一个新的Activity,需要对Activity类进行扩展,在新类定义用户界面并实现新的功能. 视图是用来显示数据和提高用户交互的Ui控件.Android提供了多个布局类, ...
- android退出登陆后,清空之前所有的activity,进入登陆主界面
如题: android退出登陆后,清空之前所有的activity,进入登陆主界面 在退出登陆时只需要增加一个intent标志 Intent intent_login = new Intent(); i ...
- Android基础部分再学习---activity的状态保存
主要是bundle 这个參数 參考地址:http://blog.csdn.net/lonelyroamer/article/details/18715975 学习Activity的生命周期,我们知 ...
- Android Activity的生命周期简单总结
Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...
- Android Activity的生命周期详解
应用程序中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监听并处理用户的事件做出响应. Activity之间通过Intent进行通信.在Intent 的描述结构中,有两个最 ...
- Activity具体解释(生命周期、以各种方式启动Activity、状态保存,全然退出等)
一.什么是Activity? 简单的说:Activity就是布满整个窗体或者悬浮于其它窗体上的交互界面.在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的Ac ...
- Activity详细解释(生命周期、以各种方式启动Activity、状态保存,等完全退出)
一.什么是Activity? 简单的说:Activity或者悬浮于其它窗体上的交互界面. 在一个应用程序中通常由多个Activity构成.都会在Manifest.xml中指定一个主的Activity, ...
随机推荐
- kindle使用参考
转载链接:http://blog.sina.com.cn/nuanfengjia 今天买的kindle499刚刚到货了,体验略差,还有一个就是无按键,完全不会玩,只能自己慢慢摸索了. [新Kindle ...
- 在JavaScript中创建命名空间的几种写法
在JavaScript中全局变量经常会引起命名冲突,甚至有时侯重写变量也不是按照你想像中的顺序来的,可以看看下面的例子: var sayHello = function() { return 'Hel ...
- MySQL外键的作用和创建
MySQL外键的作用: 保持数据一致性,完整性,主要目的是控制存储在外键表中的数据.使两张表形成关联,外键只能引用外表中列的值! 我们来建两个表 CREATE TABLE `example1` ( ` ...
- vs找不到svn源代码管理插件之我见
使用svn要安装两个文件,一个客户端:TortoiseSVN-1.8.msi,一个插件:AnkhSvn-2.5.msi:两个都安装好之后,在vs的tool(工具)选项卡中,选择自定义,然后选择sour ...
- 转载 C语言中volatile关键字的作用
一.前言 1.编译器优化介绍: 由于内存访问速度远不及CPU处理速度,为提高机器整体性能,在硬件上引入硬件高速缓存Cache,加速对内存的访问.另外在现代CPU中指令的执行并不一定严格按照顺序执行,没 ...
- linkButton
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- 更改 android realtek的系统权限
由于在 在删除系统的文件时候出现 Read-only file system,所以要获取权限. 推出shell adb mount mount -o rw,remount /system 就可以了
- ListView控件的Insert、Edit和Delete功能(第二部分)
本系列文章将通过一个简单的实例,结合我自己使用ListView的情况,展示如何用ASP.NET 3.5 ListView控件进行基本的Insert.Edit和Delete操作. 系统要求: Windo ...
- WNDCLASS 窗口类结构
Windows 的窗口总是基于窗口类来创建的,窗口类同时确定了处理窗口消息的窗口过程(回调函数). 在创建应用程序窗口之前,必须调用 RegisterClass 函数来注册窗口类.该函数只需要一个参数 ...
- Automatic Trading
Automatic Trading A brokerage firm is interested in detecting automatic trading. They believe that a ...