继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android零基础入门第74节:Activity启动和关闭

一只斗牛犬
关注TA
已关注
手记 338
粉丝 49
获赞 300

 上一期我们学习了Activity的创建和配置,当时留了一个悬念,如何才能在默认启动的Activity中打开其他新建的Activity呢?那么本期一起来学习如何启动和关闭Activity。

                                              

一、概述

    经过前面内容的学习,知道一个Android应用通常都会包含多个Activity,但只有一个Activity 会作为程序的入口——当该Android应用运行时将会自动启动并执行该Activity。至于应用中的其他Activity,通常都由入口 Activity启动,或由入口 Activity启动的Activity启动。

    Activity启动其他Activity有如下两个方法。

·         startActivity(Intent intent):启动其他 Activity。

·         startActivityForResult(Intent intent, int requestCode):以指定的请求码(requestCode)启动Activity,而且程序将会获取新启动的Activity返回的结果(通过重写 onActivityResult(..)方法来获取)。其中requestCode参数代表了启动Activity的请求码,该请求码的值由开发者根据业务自行设置,用于标识请求来源。

    上面两个方法都用到了 Intent参数,Intent是Android应用里各组件之间通信的重要方式,一个Activity通过Intent来表达自己“意图”——想要启动哪个组件,被启动的组件既可是 Activity组件,也可是Service组件。

    关于Intent的详细使用会在后续部分学习,这里先知道创建Intent对象的两个方法,在开发中根据自己喜好选择使用其中一种即可,具体代码如下。

1.    // 方式一

2.    // 创建Intent对象

3.    Intentintent1=newIntent();

4.    // 设置需要启动的Activity,以及要启动Activity的上下文环境

5.    intent1.setClass(this,MyActivity.class);

6.     

7.     

8.    // 方式二

9.    // 直接创建Intent对象,包含要启动的Activity信息

10. Intentintent2=newIntent(this,MyActivity.class);

    Android为关闭Activity准备了如下两个方法。

·         finish():结束当前 Activity。

·         finishActivity(int requestCode):结束以 startActivityForResult(Intent intent, int requestCode)方法启动的 Activity。

二、示例

    接下来通过一个示例程序来学习Activity的启动和关闭。

    仍然使用上期创建的ActivitySample工程,为了便于代码管理,这里新建一个activitystartupshutdown的Module。该Module一共包含了2个Activity,并在AndroidManifest清单文件中配置两个Activity。

    其中第一个Activity的界面布局非常简单,只包含一个按钮和文本提示,其中按钮的作用主要是启动第二个Activity。第一个Activity对应的布局文件activity_main.xml的代码如下:

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

                android:layout_width="match_parent"

                android:layout_height="match_parent" >

    <Button

        android:id="@+id/start_btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="启动第二个Activity"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:text="我是MainActivity" />

</RelativeLayout>

    第二个Activity对应的布局也比较简单,包含3个按钮和一个文本提示,其中一个按钮用于简单的返回上一个Activity,第二个按钮用于结束自己并返回上一个Activity,第三个按钮直接结束自己。第二个Activity对应的布局文件activity_second.xml的代码如下:

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

                android:layout_width="match_parent"

                android:layout_height="match_parent" >

    <Button

        android:id="@+id/back_btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="返回"/>

    <Button

        android:id="@+id/back_close_btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/back_btn"

        android:text="返回并关闭自己"/>

    <Button

        android:id="@+id/close_btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/back_close_btn"

        android:text="关闭自己"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:text="我是SecondActivity" />

</RelativeLayout>

    AndroidManifest清单文件的Activity配置如下:

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

          package="com.jinyu.cqkxzsxy.android.activitystartupshutdown">

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN"/>

 

                <category android:name="android.intent.category.LAUNCHER"/>

            </intent-filter>

        </activity>

        <activity android:name=".SecondActivity">

        </activity>

    </application>

</manifest>

    然后编辑第一个Activity,MainActivity的代码如下:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

package com.jinyu.cqkxzsxy.android.activitystartupshutdown;

 

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

 

/**

 * @创建者 鑫鱻

 * @描述 Android零基础入门到精通系列教程,首发微信公众号ShareExpert

 */

public class MainActivity   extends AppCompatActivity   {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        //   获取应用程序中的启动按钮

        Button   startBtn = (Button) findViewById(R.id.start_btn);

        //   为启动按钮绑定事件监听器

        startBtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View source) {

                //   创建需要启动的Activity对应的Intent

                Intent   intent = new Intent(MainActivity.this,   SecondActivity.class);

                //   启动intent对应的Activity

                startActivity(intent);

            }

        });

    }

}

    接着编辑第二个Activity,SecondActivity的代码如下:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

package com.jinyu.cqkxzsxy.android.activitystartupshutdown;

 

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

 

/**

 * @创建者 鑫鱻

 * @描述 Android零基础入门到精通系列教程,首发微信公众号ShareExpert

 */

public class SecondActivity   extends AppCompatActivity   {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_second);

 

        //   获取应用程序中的返回按钮

        Button   backBtn = (Button) findViewById(R.id.back_btn);

        //   获取应用程序中的返回关闭按钮

        Button   backCloseBtn = (Button) findViewById(R.id.back_close_btn);

        //   获取应用程序中的关闭按钮

        Button   closeBtn = (Button) findViewById(R.id.close_btn);

 

        //   为返回按钮绑定事件监听器

        backBtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View source) {

                //   获取启动当前Activity的上一个Intent

                Intent   intent = new Intent(SecondActivity.this,   MainActivity.class);

                //   启动intent对应的Activity

                startActivity(intent);

            }

        });

        //   为返回关闭按钮绑定事件监听器

        backCloseBtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View source) {

                //   获取启动当前Activity的上一个Intent

                Intent   intent = new Intent(SecondActivity.this,   MainActivity.class);

                //   启动intent对应的Activity

                startActivity(intent);

                //   结束当前Activity

                finish();

            }

        });

        //   为关闭按钮绑定事件监听器

        closeBtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View source) {

                //   结束当前Activity

                finish();

            }

        });

    }

}

    上面程序中前两个按钮的监听器里的处理代码只有一行区别:finish(),如果有这行代码,则表明该Activity会结束自己。

    运行程序,可以看到下图左侧所示界面。

    点击MainActivity中的启动按钮,可以启动SecondActivity,如上图右侧所示。

    经过操作SecondActivity中的三个按钮,会发现都能回到MainActivity,大家可以思考一下这里有什么区别呢?

    今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

原文链接:http://www.apkbus.com/blog-205190-75842.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP