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。的更多相关文章

  1. Android Activity中状态保存机制

    在Activity中保存用户的当前操作状态,如输入框中的文本,一般情况下载按了home键后,重新进入文本框中的东西会丢下,所以我们要保存当前页面信息,如在写短信的时候接到一个电话,那么当你接电话的时候 ...

  2. android activity状态的保存

    今天接到一个电面,途中面试官问到一个问题,如果一个activity在后台的时候,因为内存不足可能被杀死,在这之前如果想保存其中的状态数据,比如说客户填的一些信息之类的,该在哪个方法中进行. onSav ...

  3. 【Android4高级编程笔记】深入探讨Android Activity

    创建Activity 要创建一个新的Activity,需要对Activity类进行扩展,在新类定义用户界面并实现新的功能. 视图是用来显示数据和提高用户交互的Ui控件.Android提供了多个布局类, ...

  4. android退出登陆后,清空之前所有的activity,进入登陆主界面

    如题: android退出登陆后,清空之前所有的activity,进入登陆主界面 在退出登陆时只需要增加一个intent标志 Intent intent_login = new Intent(); i ...

  5. Android基础部分再学习---activity的状态保存

    主要是bundle   这个參数 參考地址:http://blog.csdn.net/lonelyroamer/article/details/18715975 学习Activity的生命周期,我们知 ...

  6. Android Activity的生命周期简单总结

    Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...

  7. Android Activity的生命周期详解

    应用程序中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监听并处理用户的事件做出响应. Activity之间通过Intent进行通信.在Intent 的描述结构中,有两个最 ...

  8. Activity具体解释(生命周期、以各种方式启动Activity、状态保存,全然退出等)

    一.什么是Activity? 简单的说:Activity就是布满整个窗体或者悬浮于其它窗体上的交互界面.在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的Ac ...

  9. Activity详细解释(生命周期、以各种方式启动Activity、状态保存,等完全退出)

    一.什么是Activity? 简单的说:Activity或者悬浮于其它窗体上的交互界面. 在一个应用程序中通常由多个Activity构成.都会在Manifest.xml中指定一个主的Activity, ...

随机推荐

  1. Bother

    Bother Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  2. FUSE

    FUSE is particularly useful for writing [ vritual ] file system. Unlike traditional filesystem that ...

  3. 分布式事务实现-Spanner

    Spanner要满足的external consistency 是指:后开始的事务一定可以看到先提交的事务的修改.所有事务的读写都加锁可以解决这个问题,缺点是性能较差.特别是对于一些workload中 ...

  4. CentOS7 PostgreSQL 安装

    PostgreSQL安装 安装使用yum安装 (找源 http://yum.postgresql.org/) yum install https://download.postgresql.org/p ...

  5. a标签无跳转的死链接

    <a href="#" onclick="return false;">link1</a> <a href="javas ...

  6. mongodb常见问题

    1.count统计结果错误 这是由于分布式集群正在迁移数据,它导致count结果值错误,需要使用aggregate pipeline来得到正确统计结果,例如: db.collection.aggreg ...

  7. linux文件系统命令

    1. df df -h 2.查看文件系统的类型 df -T tmpfs文件系统所知道的就是它正在使用某种形式的虚拟内存.tmpfs是一种基于内存的文件系统.而tmpfs是一个文件系统,并不是块设备,只 ...

  8. C++函数后面的throw()

    看CImage函数实现的时候发现了这么个东东  inline HBITMAP CImage::Detach() throw() 它是函数提供者和使用者的一种君子协定,标明该函数不抛出任何异常. 之所以 ...

  9. HDU 5455 Fang Fang 水题,但题意描述有问题

    题目大意:f[1]=f,f[2]=ff,f[3]=ffc,以后f[n]每增加1,字符串增加一个c.给出一个字符串,求最少有多少个f[]组成.(字符串首尾相连,比如:ffcf可看做cfff) 题目思路: ...

  10. C语言根据函数名调用对应的函数

    通过函数指针定义,调用时加上参数 struct Command { const char *name; const char *desc; // return -1 to force monitor ...