案例一:图片获得焦点事件
MainActivity
package com.example.onfocuschagelistenerdemo;
import java.util.Iterator;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity implements OnFocusChangeListener{
//焦点事件
private TextView show ;
ImageButton [] imageButtons = new ImageButton[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show =(TextView)findViewById(R.id.show);
imageButtons[0] =(ImageButton)this.findViewById(R.id.Imgshow1);
imageButtons[1]= (ImageButton)this.findViewById(R.id.imgshow2);
imageButtons[0].setOnFocusChangeListener(this);
imageButtons[1].setOnFocusChangeListener(this);
/*
* for (ImageButton imagebutton : imageButtons) {
* imagebutton.setOnFocusChangeListener(this);
* }
*for (int i = 0; i < imageViews.length; i++) {
* //普通循环
* }
* for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
* type type = (type) iterator.next();
* //遍历 针对于Collection 集合
* }
* **/
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(v.getId()==R.id.Imgshow1){
this.show.setText("你选中了羊");
}else if(v.getId()==R.id.imgshow2){
this.show.setText("你选中的是够");
}
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageButton
android:id="@+id/Imgshow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:src="@drawable/png1" />
<!--android:focusableInTouchMode="true" 组件在进入触摸模式是否可以获得焦点 -->
<ImageButton
android:id="@+id/imgshow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:src="@drawable/png2"
/>
</LinearLayout>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
效果图
案例二:焦点事件的验证
MainActivity
package com.example.onfocuschangelinstenerdemo2;
import android.view.View.OnFocusChangeListener;
public class MainActivity extends Activity {
private EditText ed1;
private TextView show ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show=(TextView)findViewById(R.id.show);
ed1=(EditText)findViewById(R.id.et1);
ed1.setOnFocusChangeListener(new OnFocusChangeListenerText());
}
class OnFocusChangeListenerText implements OnFocusChangeListener{
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(v.getId()==MainActivity.this.ed1.getId()){
if(hasFocus){
MainActivity.this.show.setText("文本输入获得焦点的组件是ed1");
}else {
MainActivity.this.show.setText("你ed1中输入的是合法");
}
}else{
if(hasFocus){
MainActivity.this.show.setText("文本输入获得焦点的组件是ed2");
}else {
MainActivity.this.show.setText("你ed2中输入的是合法");
}
}
}
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入内容"
/>
<EditText
android:id="@+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入内容"
/>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
SharedPreferencs
Android 操作系统提供五种存储方式 , SharedPreference ,文件存储,SQLite数据存储,ConnectionProvider存储,网络存储。
SharedPreferencs保存数据
package com.example.gdsharepreferfacedemo;
public class MainActivity extends Activity {
//SharedPreferences 文件保存
private static final String FILENAME ="gangdan";//文件名称
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences share =super.getSharedPreferences(FILENAME, Activity.MODE_PRIVATE);
//指定文件的名字和编辑的模式
SharedPreferences.Editor edit =share.edit();//获得edit()接口
edit.putString("gqy", "guoqingyu");//保存字符串类型;
edit.putInt("年龄", 18);//保存整型
edit.commit();//提交
}
}
SharedPerferences 获取数据
package com.example.hxsharedpreference;
public class OtherActivity extends Activity {
private TextView msgName, msgAge;
private static final String FILENAME="xuesheng";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
msgName=(TextView)findViewById(R.id.msgName);
msgAge=(TextView)findViewById(R.id.msgAge);
SharedPreferences sharedPreferences =
getSharedPreferences(FILENAME, Activity.MODE_PRIVATE);
msgName.setText(sharedPreferences.getString("Name", "没有此用户"));
int age= sharedPreferences.getInt("Age", 0);
String msg= Integer.toString(age);
msgAge.setText(msg );
}
}
注:TextView.SetText()这能设置String类型
把整形转化成String类型两种常用方式
Int i=10;
String msg= Integer.toString(i);
String xx = String.valueOf(1);
File存储
FileOutStream(向文件写数据)
package com.example.hxfile;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class MainActivity extends Activity {
private static final String FILENAME="file.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FileOutputStream out=null;
try {
out =openFileOutput(FILENAME, Activity.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintStream output= new PrintStream(out);
output.print("北京中软国际");
output.print("哈尔滨学院");
}
}
FileInputStream(从文件中读取数据)
package com.example.hxfile;
public class OtherActivity extends Activity {
private TextView showmsg =null;
private static final String FILENAME="file.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
showmsg=(TextView)findViewById(R.id.msg);
FileInputStream input=null;
try {
input=openFileInput(FILENAME);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner scanner = new Scanner(input);
while(scanner.hasNext()){
showmsg.append(scanner.next()+"\n");
}
}
}
JSON
JSON 常用的几个对象 JSONObject ,JSONArray(), JSONStringer和JSONException
Json数据格式
Json对象是一个无序的“名称/值”对的集合
如1{“name”:“郭庆雨”,
“age”:18}
如2 {“syudent“ [
{ “name”:”郭庆雨” ,“age“:100}
{ “ name”:”张三”,”age”:100}
] }
JSONObject allData = new JSONObject();
allData.put(“name“,”郭庆雨“)
//用put 方法(Key,value)形式添加和Map集合是一样的
allData.toString();
//转化成JSONObject对象转化成String 类型
//JSONObject 存储是一个对象
实例一:举例说明什么是JSONObject
JSONObject alljson = new JSONObject();
try {
alljson.put("person", "郭庆雨");
System.out.println(alljson.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
结果 {“person”,”郭庆雨”}
实例二:举例说明什么是JSONObject和JSONArray
String data []={"郭庆雨","18","北京"};
JSONObject alljson = new JSONObject();
JSONArray sing = new JSONArray();
for(int x=0;x<data.length;x++){
JSONObject msg = new JSONObject();
try {
msg.put("name",data[x] );
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sing.put(msg);
}
try {
alljson.put("url", sing);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(alljson);
结果:{"person":[{"name":"郭庆雨"},{"name":"文章"},{"name":"同大卫"}]}
实例三:开发常用实例
一个Json工具包 JsonTooles 一个 实体类工具包Person 一个JSON服务包JsonServices 一个Activity包jsonActivity
JsonTooles 包
public class JsonTooles {
public static String createJsonString (String key ,Object object){
//解析其他的Object 类型的数据 除了List内含有对象的数据
JSONObject allDataString = new JSONObject();
try {
allDataString.put(key, object);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return allDataString.toString();
}
public String createJsonList(String key ,List<Person> list) {
//获取List内部是一个对象
JSONObject allData = new JSONObject();
JSONArray sing= new JSONArray();
Person person=null;
for(int x=0;x<list.size();x++){
person =list.get(x);
JSONObject msg = new JSONObject();
try {
msg.put("name", person.getName());
msg.put("age", person.getAge());
msg.put("address", person.getAddress());
sing.put(msg);
allData.put(key, sing) ;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return allData.toString();
}
}
JSON 后台解析
MainActivity
public class MainActivity extends Activity {
private Button preson,presons,listString ,listMap;
private static final String TAG ="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preson= (Button)findViewById(R.id.person);
//获取文本服务器的personJson
preson.setOnClickListener(new OnPersonListenner());
}
class OnPersonListenner implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(){
public void run() {
HttpTooles httpTooles = new HttpTooles();
String jsonString=httpTooles.getJsonContent("http://192.168.1.108:8080/jsonProject2/Action?type=person");
JsonTooles jsonTooles = new JsonTooles();
Person person= jsonTooles.getPerson(jsonString);
//System.out.println(str);
System.out.println(person.getAddress());
System.out.println(person.getAge());
System.out.println(person.getName());
Log.i(TAG, person.toString());
};
}.start();
}
}
}
HttpTooles
public class HttpTooles {
public HttpTooles() {
super();
// TODO Auto-generated constructor stub
}
public static String getJsonContent(String urlpath){
String str=null;
try {
URL url = new URL(urlpath);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setRequestMethod("GET");
//httpURLConnection.getContentEncoding();
httpURLConnection.setDoInput(true);//允许读服务器内容
int code =httpURLConnection.getResponseCode();
if(code==200){
//当这个状态码是200的时候说明服务器状态已经准备好了 可以获得一个流
InputStream in= httpURLConnection.getInputStream();
BufferedReader buf =
new BufferedReader(new InputStreamReader(new BufferedInputStream(in)));
str =buf.readLine();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
JsonTooles
public class JsonTooles {
public JsonTooles() {
super();
// TODO Auto-generated constructor stub
}
public static Person getPerson(String jsonString){
Person person = new Person();
try {
JSONObject jsonperson = new JSONObject(jsonString);
JSONObject personObject = jsonperson.getJSONObject("person");
person.setAddress(personObject.getString("address"));
person.setAge(personObject.getInt("age"));
person.setName(personObject.getString("name"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return person;
}
}
实体类工具包Person
public class Person {
private String name ;
private int age ;
private String address;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address
+ "]";
}
}
JSON服务包JsonServices
public class JsonService {
public JsonService() {
}
public Person getPerson(){
//含有一个对象
Person person = new Person("郭庆雨", 18,"北京");
return person;
}
public List <Person> getListperson(){
//含有多个对象的List集合
List<Person> list = new ArrayList<Person>();
Person person = new Person("郭庆雨", 18,"北京");
Person person1 = new Person("赵薇", 18,"南京");
Person person2 = new Person("文章", 18,"天津");
list.add(person);
list.add(person1);
list.add(person2);
return list;
}
public List <String >getListString(){
//普通的List集合
List<String> list = new ArrayList<String>();
list.add("北京");
list.add("上海");
list.add("南京");
return list;
}
public List<Map<String ,Object>> getListMaps(){
//list集合的Map集合
List <Map<String ,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map= new HashMap<String,Object>();
map.put("地址", "北京");
Map<String,Object> map2= new HashMap<String,Object>();
map2.put("地址", "北京");
list.add(map);
list.add(map2);
return list;
}
}
Activity包jsonActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JsonService service = new JsonService();
List <Person> list=service.getListperson();
JsonTooles jsontooles = new JsonTooles();
String str= jsontooles.createJsonString("persons",list);
System.out.println(str);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Intent
认识Intent
Intent传值
Intent值的回调
Intent深入
打开网页
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开网页" />
</LinearLayout>
MainActivity
package com.example.intentdemo2;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button but ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button)findViewById(R.id.mybut);
but.setOnClickListener(new OnClickListenerImp());
}
class OnClickListenerImp implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
MainActivity.this.startActivity(intent);
}
}
}
Intent打电话
案例一:调用拨号键
MainActivity
package com.example.intentdemo4;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button but;
private EditText tel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button)findViewById(R.id.myBut);
tel=(EditText)findViewById(R.id.tel);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String telStr= MainActivity.this.tel.getText().toString();
Uri uri=Uri.parse("tel:"+telStr);
Intent intent= new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(uri);
MainActivity.this.startActivity(intent);
}
});
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/myBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打电话" />
</LinearLayout>
AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>
案例二:直接打电话
MainActivity
package com.example.intentdemo4;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button but;
private EditText tel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button)findViewById(R.id.myBut);
tel=(EditText)findViewById(R.id.tel);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String telStr= MainActivity.this.tel.getText().toString();
Uri uri=Uri.parse("tel:"+telStr);
Intent intent= new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(uri);
MainActivity.this.startActivity(intent);
}
});
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/myBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="直接打电话" />
</LinearLayout>
AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>
Activity生命周期
什么是Activity
- Activity 实际上是一个人机的交互程序,用于存放各个显示控件,也是Android的基本组成部分
- 每一个继承了android.app.Activity的java类都将成为一个Activity程序。
- 创建Activity的要点
创建Activity的要点
- 1,一个Activity就是一个类,并且这个类要继承Activity
- 2,需要复写Oncreate 方法
- 3,每一个Activity都需要在AndroidManifest.xml 文件当中进行配置
- 4,为Activity添加必要的控件
Activit七个生命周期函数
- Public class Activity extends ApplicationContext{
- Protected void onCreate ()
- 一个不可以关闭的方法;当Activity程序启动之后会首次调用此方法。
- Protected void onStart ()
- 一个不可以关闭的方法;当用户第一次显示界面时调用此方法。
- Protected void onRestart ()
- 一个不可以关闭的方法;当Activity 程序停止后再次显示给用户是调用。
- Protected void onResume ()
- 一个不可以关闭的方法;当获得用户焦点时候调用此方法
- Protected void onPause ()
- 一个可以关闭的方法;当启动其他Activity程序时调用此方法用于进行数据的提交,动画处理等操作
- Protected void onStop ()
- 一个可以关闭的方法;当一个Activity程序完全不可见时调用此方法,此时并不会销毁Activity程序
- Protected void onDestroy ()
- 一个可以关闭的方法;程序被销毁时调用,当调用finish()方法或系统资源不够用时将调用此方法。
- }
Activity流程图
案例一:
Mainactivity
package com.example.gdactivity2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("FriestActivity------->onCreate()");
/*
* 第一次创建时候 加载整个组件*/
start=(Button)findViewById(R.id.starOtherActivity);
start.setOnClickListener(new ButttonClick());
}
class ButttonClick implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);
MainActivity.this.startActivity(intent);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("FriestActivity------->onDestroy()");
super.onDestroy();
/*
* 当调用finish()方法或系统资源不够用时将调用此方法**/
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("FriestActivity------->onPause()");
/*
* 暂停当前方法
* **/
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("FriestActivity------->onRestart()");
super.onRestart();
/*
* 当Activity重新被调用时候用此方法**/
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("FriestActivity------->onResume() ");
/*
* 当用户在点击或是使用组件时候获取焦点
*
* **/
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("FriestActivity------->onStart() ");
/*
* 当用户第一次看见模拟器出现组件时候被调用
* **/
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("FriestActivity------->onStop()");
/*当Activity全部不可见状态是 调用此方法
* **/
super.onStop();
}
}
OtherActivity
package com.example.gdactivity2;
import android.app.Activity;
import android.os.Bundle;
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
System.out.println("OtherActivity------->onCreate()");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("OtherActivity------->onDestroy()");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("OtherActivity------->onPause()");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("OtherActivity------->onRestart()");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("OtherActivity------->onResume()");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("OtherActivity------->onStart()");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("OtherActivity------->onStop()");
}
}
AndroidManifest
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gdactivity4.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.gdactivity4.SecodActivity"
android:label="@string/title_activity_secod"
android:theme="@android:style/Theme.Dialog"
>
<!--@android:style/Theme.Dialog" 定义第二个Activity为敞口模式 -->
</activity>
</application>
运行本节案例一
- 操作一 :首先启动程序
- 结果一: 启动了FirstActivity的onCreate onStart onResume
- 操作二:点击启动第二个Activity
- 结果二:1: FirstActivity的onPause ,2:SecondActivity onCreate onStart onResume,3:FirstActivity onStop
- 操作三:点击back按钮的时候
- 结果三:SecondActivity onPause FirstActivity onRestart onStart onResume SecondActivity onStop onDestory
注意:什么时候调用onDestroy方法
- 1 在一个Activity调用了finish() 方法
(此效果在下一节案例1给大家展示)
- 2 资源不够或是系统认为此Activity没有用的时候系统调用onDestroy方法