一、Activity的生命周期。
我们知道一个Activity代表一个页面。其中,在我们创建完成Android项目的时候,不难发现Activity的OnCreate方法是页面的入口函数。下图是为Activity的生命周期图:
Android studio活动在其生命周期的四种状态分别是:
运行状态
暂停状态
停止状态
销毁状态
Activity与生命周期的有关的几个方法:
onCreate方法:创建页面。把页面上的各个元素加载到内存中。
onStart方法:开始页面。把页面显示在屏幕上。
onResume方法:恢复页面。让页面在屏幕上活动起来,例如开机动画,开始任务。
onPause方法:暂停页面。让页面在屏幕上的动作停下来。
onStop方法:停止页面。把页面从内存上撤下来。
onDestroy方法。销毁页面。把页面琮内存中清除掉。
onRestart方法。重新加载内存中的页面数据。
Task是一个存放Activity的一个栈,遵循先进后出的原则,分有压栈(进栈)和盘栈(出栈)两个操作。Activity都会作为元素存放在Task里面,遵循先进后出的原则。每运行一个Activity都会将此Activity压栈,从第一个运行Activity中跳转到第二个Activity,第二个Activity会压栈到第一个Activity的顶部,然后把运行在第二个Activity、第三个、第四个等依次类推。如果在跳转Activity之前当前的Activity没有执行finish()方法,此Activity就不会在Task中被销毁。
二、使用Intent传递消息。
1、下面用一个实例来实现向下一个Activity传递参数。
实现的页面效果是:
当我们点击提交的时候,进行跳转页面,并在第二个页面上显示具体填写的内容。
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.edu.hznu.ex4_1.MainActivity">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请填写以下个人信息"
android:textSize="40sp"
android:textColor="#ff0000"
android:layout_gravity="center"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="姓名"/>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="年龄"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radiogroup">
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:checked="true"
android:text="男"
android:id="@+id/rediobutton1"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="女"
android:id="@+id/radiobutton2"/>
</RadioGroup>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="电话"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择你的爱好:"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读书"/>
<CheckBox
android:id="@+id/travel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旅游"/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="30sp"/>
</LinearLayout>
MainActivity.java
package cn.edu.hznu.ex4_1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.io.Serializable;
public class MainActivity extends AppCompatActivity {
private EditText editText1,editText2,editText3;
private RadioButton rediobutton1,rediobutton2;
private CheckBox checkBox1,checkBox2,checkBox3;
private Button button;
private RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name=editText1.getText().toString();
String age=editText2.getText().toString();
RadioButton rb = (RadioButton)findViewById(radiogroup.getCheckedRadioButtonId());
String sex=rb.getText().toString(); //获取性别
String phone=editText3.getText().toString();
StringBuilder str = new StringBuilder();
if(checkBox1.isChecked()){
str.append(checkBox1.getText().toString()+" ");
}
if(checkBox2.isChecked()){
str.append(checkBox2.getText().toString()+" ");
}
if(checkBox3.isChecked()){
str.append(checkBox3.getText().toString()+" ");
}
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("name",name);
intent.putExtra("age",age);
intent.putExtra("sex",sex);
intent.putExtra("phone",phone);
intent.putExtra("str", (Serializable) str);
startActivity(intent);
}
});
}
public void init(){
editText1=(EditText) findViewById(R.id.name);
editText2=(EditText) findViewById(R.id.age);
editText3=(EditText) findViewById(R.id.phone);
radiogroup=(RadioGroup) findViewById(R.id.radiogroup);
button=(Button) findViewById(R.id.button);
checkBox1=(CheckBox) findViewById(R.id.sport);
checkBox2=(CheckBox) findViewById(R.id.read);
checkBox3=(CheckBox) findViewById(R.id.travel);
}
}
下面是第二个Activity
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.edu.hznu.ex4_1.Main2Activity">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#ff0000"
android:text="姓名"/>
<TextView
android:id="@+id/age"
android:layout_width="match_parent"
android:textSize="30sp"
android:textColor="#ff0000"
android:layout_height="wrap_content"
android:text="年龄"/>
<TextView
android:id="@+id/sex"
android:layout_width="match_parent"
android:textSize="30sp"
android:textColor="#ff0000"
android:layout_height="wrap_content"
android:text="性别"/>
<TextView
android:id="@+id/phone"
android:layout_width="match_parent"
android:textSize="30sp"
android:textColor="#ff0000"
android:layout_height="wrap_content"
android:text="电话"/>
<TextView
android:id="@+id/str"
android:layout_width="match_parent"
android:textSize="30sp"
android:textColor="#ff0000"
android:layout_height="wrap_content"
android:text="爱好"/>
</LinearLayout>
Main2Activity.java
package cn.edu.hznu.ex4_1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
private TextView textView[]=new TextView[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
init();
Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");
String age=bundle.getString("age");
String sex=bundle.getString("sex");
String phone=bundle.getString("phone");
String str=bundle.getString("str");
textView[0].setText("你的姓名:"+name);
textView[1].setText("你的年龄:"+age);
textView[2].setText("你的性别:"+sex);
textView[3].setText("你的电话:"+phone);
textView[4].setText("你的爱好有:"+str);
}
public void init(){
textView[0]=(TextView) findViewById(R.id.name);
textView[1]=(TextView) findViewById(R.id.age);
textView[2]=(TextView) findViewById(R.id.sex);
textView[3]=(TextView) findViewById(R.id.phone);
textView[4]=(TextView) findViewById(R.id.str);
}
}
当填入信息点击提交按钮:
在我们第一个页面中:
我们在这里我们引入Bundle的概念,我们可以这样来理解Bundle,理解为超市的寄包柜或者快递收件柜,,大小包裹有Bundle统一存取,方便又安全。
Bundle内部用于存取数据的实质结构是Map映射,可以添加元素,删除元素,判断元素是否存在。我们只需把Bundle全部打包好只需调用一次putExtras方法,把Bundle全部取出来也只需要调用一次getExtras即可。
那么上面的代码我们变为:
2、向上一个Activity返回参数。
处理的步骤如下:
(1)前一个页面打包好请求数据,调用方法startActivityForResult(Intent intent,int requestCode),表示需要处理结果数据,第二个参数表示请求编号,用于标识每次请求的唯一性。
(2)后一个页面接收请求数据,进行相应的处理。
(3)后一个页面在返回前一个页面时,打包应答数据并调用setResult方法返回信息,setResult的第一个参数表示应答代码(成功还是失败)。示例:在第一个页面点击“点击按钮”跳转第二个页面:
选择后,跳转原来页面,并显示。
activity_main.xm
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.edu.hznu.ex4_2.MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的选择是:"
android:layout_gravity="center"
android:textSize="30sp"
android:textColor="#0004ff"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击"
android:textSize="30sp"
android:textColor="#ff0000"
/>
</LinearLayout>
MainActivity.java
package cn.edu.hznu.ex4_2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textview);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent,1);
}
});
}
/**requestCode:请求编号:判断对应的是哪次请求
* resultCode:应答代码:判断后一个页面是否处理成功
*
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
if(data!=null&&resultCode==RESULT_OK){
String value=data.getStringExtra("value");
textView.setText(value);
}
break;
case 2:
break;
}
}
}
第二个页面:
activity_two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.edu.hznu.ex4_2.SecondActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview"
></ListView>
</RelativeLayout>
TwoActivity.java
package cn.edu.hznu.ex4_2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SecondActivity extends AppCompatActivity {
private ListView listView;
private String[] data={"江苏省","河南省","安徽省","江西省"};
private ArrayAdapter<Object> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
listView=(ListView)findViewById(R.id.listview);
adapter=new ArrayAdapter<Object>(SecondActivity.this,R.layout.item_select,data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String value=data[position];
Intent intent=new Intent();
intent.putExtra("value",value) ;
setResult(RESULT_OK,intent); //RESULT_OK:-1
finish();
}
});
}
}
效果: