Android TaskStack

Android TaskStack

本文主要简单介绍安卓任务栈 Android TaskStack:

  • Android TaskStack符合Stack“先进后出的原则”
  • 时间:2016年7月17日 15:04:51
  • 内容:验证Android TaskStack
  • 结果:且看下方

代码块

TestActivity.java

package com.czk.testview.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.Toast;

import com.czk.testview.R;
/**
 * 时间:2016年7月17日 14:06:26
 * @author Notzuonotdied
 * 内容:测试TaskStack
 * 测试结果:
 *      |--最终结果是:
 *      |--Activity之间的跳转是按照Stack的形式来存储的,
 *      |--满足“先进后出”的条件。
 *      |--同时,Activity的跳转是在同一个task中进行的。
 * */
public class TestActivity extends Activity {
    private Button btn_the_TestActivity;
    private Button btn_the_TestActivity01;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_test);
        initView();
    }

    protected void onDestroy() {
        super.onDestroy();
        initInfo();
    }

    private void initInfo() {
        String testString = "onDestroy TaskId=" + getTaskId() + ".TestActivity";
        Toast.makeText(TestActivity.this, testString,
                Toast.LENGTH_SHORT).show();
        Log.i("TestActivity", testString);
    }

    private void initView() {
        btn_the_TestActivity = (Button) findViewById(R.id.btn_the_TestActivity);
        btn_the_TestActivity.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String testString = "TaskId=" + getTaskId() + ".TestActivity";
                Toast.makeText(TestActivity.this, testString,
                        Toast.LENGTH_SHORT).show();
                Log.i("TestActivity", testString);
                Intent intent = new Intent(TestActivity.this,
                        TestActivity.class);
                startActivity(intent);
            }
        });
        btn_the_TestActivity01 = (Button) findViewById(R.id.btn_the_TestActivity01);
        btn_the_TestActivity01.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String testString = "TaskId=" + getTaskId() + ".TestActivity01";
                Toast.makeText(TestActivity.this, testString,
                        Toast.LENGTH_SHORT).show();
                Log.i("TestActivity01", testString);
                Intent intent = new Intent(TestActivity.this,
                        TestActivity01.class);
                startActivity(intent);
            }
        });
    }
}

TestActivity01.java

package com.czk.testview.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.Toast;

import com.czk.testview.R;

public class TestActivity01 extends Activity {
    private Button btn_the_TestActivity;
    private Button btn_the_TestActivity01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_test);
        initView();
    }

    protected void onDestroy() {
        super.onDestroy();
        initInfo();
    }

    private void initInfo() {
        String testString = "onDestroy TaskId=" + getTaskId() + ".TestActivity01";
        Toast.makeText(TestActivity01.this, testString, Toast.LENGTH_SHORT)
                .show();
        Log.i("TestActivity01", testString);
    }

    private void initView() {
        btn_the_TestActivity = (Button) findViewById(R.id.btn_the_TestActivity);
        btn_the_TestActivity.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String testString = "TaskId=" + getTaskId() + ".TestActivity";
                Toast.makeText(TestActivity01.this, testString,
                        Toast.LENGTH_SHORT).show();
                Log.i("TestActivity", testString);
                Intent intent = new Intent(TestActivity01.this,
                        TestActivity.class);
                startActivity(intent);
            }
        });
        btn_the_TestActivity01 = (Button) findViewById(R.id.btn_the_TestActivity01);
        btn_the_TestActivity01.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String testString = "TaskId=" + getTaskId() + ".TestActivity01";
                Toast.makeText(TestActivity01.this, testString,
                        Toast.LENGTH_SHORT).show();
                Log.i("TestActivity01", testString);
                Intent intent = new Intent(TestActivity01.this,
                        TestActivity01.class);
                startActivity(intent);
            }
        });
    }
}

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!--这里的android:text=".."中的“btn_the_TestActivity”本应该写在string.xml中,本处为了方便观看便写在此处!-->
    <Button 
        android:id="@+id/btn_the_TestActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn_the_TestActivity"
        android:textSize="24sp"/>
    <Button 
        android:id="@+id/btn_the_TestActivity01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn_the_TestActivity01"
        android:textSize="24sp"/>

</LinearLayout>

最终log.i输出的信息为:

–TaskId=48.TestActivity
–TaskId=48.TestActivity01
–TaskId=48.TestActivity01
–TaskId=48.TestActivity
–TaskId=48.TestActivity01
–onDestroy TaskId=48.TestActivity01
–onDestroy TaskId=48.TestActivity
–onDestroy TaskId=48.TestActivity01
–onDestroy TaskId=48.TestActivity01
–onDestroy TaskId=48.TestActivity
–onDestroy TaskId=48.TestActivity

—由上述的显示结果容易得出,在多个Activity进行切换的时候(这里的AndroidManifest.xml中采取的是默认配置),是服从Stack“先进后出”的标准的。
—此外还有配置了AndroidManifest.xml的情况,详情请见
—1.Android 四种启动模式(Launch Mode) 最直观详解
—2.Activity的Launch mode详解 singleTask正解
四种启动模式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值