一、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();
}
});
}
}
效果: