1、Task基本概念
Task就是一个任务栈,里面存放了好多Activity
2、Activity和Task之间的关系
我们通过一个demo来介绍
Demo:有三个Activity:FirstActivity、SecondActivity、ThirdActivity,FirstActivity布局里面有一个按钮,点击会跳转到SecondActivity,同样SecondActivity布局里面有一个按钮,点击会跳转到ThirdActivity,ThirdActivity布局里面有一个按钮,点击会启动打电话这样的一个Android自带的Activity
详细过程如下:
1)、当应用程序启动后,运行第一个Activity之后,该Activity对象被压入到Stack中
2)点击按钮启动SecondActivity,该SecondActivity对象被压入到Stack中,显示栈顶Activity,所以此时手机显示SecondActivity
3)点击SecondActivity中的按钮之后,启动ThirdActivity,ThirdActivity被压入到Stack中
4)点击ThirdActivity中的按钮之后,启动第四个Activity,也即是启动打电话的那个Activity
每次栈顶的Activity都是用户可见的,而非栈顶的Activity只是被栈顶的Activity遮挡住,还存活着
5)点击Back按钮,这时SMS Activity对象从栈中弹出,ThirdActivity成为栈顶Activity,对用户可见,弹出的所有Activity都会调用自己的onDestory方法
6)点击Back按钮,这时ThirdActivity对象从栈中弹出,SecondActivity成为栈顶Activity,对用户可见,弹出的所有Activity都会调用自己的onDestory方法
7)点击Back按钮,这时SecondActivity对象从栈中弹出,FirstActivity成为栈顶Activity,对用户可见,弹出的所有Activity都会调用自己的onDestory方法
代码如下:
FirstActivity.java
public class SecondActivity extends AppCompatActivity {
private Button button=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
button=(Button) findViewById(R.id.secondButton);
}
class ButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(SecondActivity.this,SecondActivity.class);
startActivity(intent);
}
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private Button button=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
button=(Button) findViewById(R.id.secondButton);
}
class ButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
}
}
}
ThirdActivity.java
public class ThirdActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
button=(Button)findViewById(R.id.thirdButton);
}
class ButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Uri uri= Uri.parse("smsto://0800000123");
Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","THE SMS TEXT ");
startActivity(intent);
}
}
}
activity_first.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="chao.pers.activity_06.FirstActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/firstButton"
android:text="ToSecondActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
activity_second.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="chao.pers.activity_06.SecondActivity">
<TextView
android:text="SecondActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/secondButton"
android:text="ToThridActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
activity_third.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="chao.pers.activity_06.ThirdActivity">
<TextView
android:text="ThridActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/thirdButton"
android:text="toSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chao.pers.activity_06">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity"></activity>
</application>
</manifest>