Android----Intent的使用

Activity是Android最吸引人的地方,它是一种可包含用户界面的组件,主要用于实现交互。通常来说Activity就是我们看见的页面。那么我们可以很自然的知道一个app可以包含很多个Activity了。那么这时候 我们就想问一下,怎么进行交互的呢,这样一来我们就知道了Intent的概念 ,接下来我们就开始研究一下intent的使用吧。

Intent的基本使用

我们手动创建两个activity,分别命名为FirstActivity和SecondActivity,分别在他们对应的layout布局代码中改为以下代码:
first_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">\

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_01"
        android:text="Button1"
        />

</LinearLayout>

second_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_02"
        android:text="Button 2"
        />

</LinearLayout>

很简单可以看出,我们只是在这两个页面中,各自添加一个按钮。

好了我们创建好了我们的activity就是页面(活动),接下来我们要进行交互了。这时候你也别忘了在AndroidManifest.xml进行修改部分配置

<activity
            android:name=".FirstActivity"
            android:label="This is FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.ACTION_START" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.activitytest.MY_CATEGORY" />
            </intent-filter>
        </activity>

我也不多说这里的配置是什么了。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);  //加载这个XML布局
        Button button1 = (Button) findViewById(R.id.button_01);  //获取Button的实例
        button1.setOnClickListener(new View.OnClickListener() {  //设置button的点击事件
            @Override
            public void onClick(View v) {
                //显示Intent的使用
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivity(intent);

//                //隐式Intent的调用
//                Intent intent = new Intent("com.example.activitytest.ACTION_START");
//                intent.addCategory("com.example.activitytest.MY_CATEGORY");
//                startActivity(intent);
            }
        });
    }

上面有两种Intent的使用,我在这里只说第一种显式的用法,不难看出,Intent里面的两个参数,第一个是我们当前页面,第二个是我们要跳转的页面。startActivity方法就是专门启动activity的,我们只需要把我们定义的intent传入进去即可。

然后就可以进行跳转了。效果图我就省略了。

当然这里的intent也可以进行别的操作,比如打开网址了,拨打电话了。我就不多说了,很简单,只需要把相应的代码改为:

Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setData(Uri.parse("http://www.baidu.com"));
 startActivity(intent); 

或者

Intent intent = new Intent(Intent.ACTION_DIAL);
 intent.setData(Uri.parse("tel:10086"));
 startActivity(intent); 

就行了。

这样我们就能完成基本的activity跳转了。但是对于进一步使用activity还是不行了,因为我们的需求有时候会需要在我们跳转的时候带着我们的参数在下一页面使用。那么我们还要怎么做呢,刚好Intent给我们封装要了api供我们使用,别急继续看下面。

Intent跳转并且传值

我们修改FirstActivity的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);  //加载这个XML布局
        Button button1 = (Button) findViewById(R.id.button_01);  //获取Button的实例
        button1.setOnClickListener(new View.OnClickListener() {  //设置button的点击事件
            @Override
            public void onClick(View v) {
                //传值
                String data = "Hello SecondActivity";
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("data",data);
                startActivity(intent);
        });
    }

在这里面我们只需要把数据存在intent里面就行了,因为在他跳转的时候会使用intent这个实例,那么自然在intent里面的所有东西都会进行对应的操作。

然后我们在SecondActivity的代码进行修改:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        Log.d(TAG, "intent的传值" + data);
    }

这样我们就完成了从FirstActivity到SecondActivity的传值了。

返回数据给上一界面

既然我们都明白了可以从FirstActivity到SecondActivity的传值了,那么相反的,能不能从SecondActivity回传给FirstActivity呢?

我们还是修改一下FirstActivity的click代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);  //加载这个XML布局
        Button button1 = (Button) findViewById(R.id.button_01);  //获取Button的实例
        button1.setOnClickListener(new View.OnClickListener() {  //设置button的点击事件
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent,1);
        });
    }

由于调用的startActivityForResult方法来情动SecondActivity,所以当我们在SecondActivity销毁会后会回调上个Activity的onActivityResult方法,所以我们还需要在FirstActivity重写onActivityResult方法来得到从SecondActivity返回的数据。等会说我们需要重写的onActivityResult方法。

我们修改SecondActivity的点击事件方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "second activity task id is::::" + getTaskId());
        setContentView(R.layout.second_layout);

        Button button2 = (Button) findViewById(R.id.button_02);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("data_return", "Hello FirstActivity");
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

可以看到我们还是构建了一个Intent,只不过这个intent仅仅是传递数据而已,没有跳转的操作,紧接着我们把数据存储带intent中,然后调用setResult方法。这个方法接收两个参数,第一个参数是活动的处理结果,一般只使用 RESULT_OK 或 RESULT_CANCELED 这
两个值,第二个就是我们的intent实例。

上面我也说了由于我们使用的startActivityForResult启动SecondActivity的,在 SecondActivity
被销毁之后会回调上一个活动的 onActivityResult方法,因此我们需要在 FirstActivity 中重
写这个方法来得到返回的数据。
所以我们要在FirstActivity的重写onActivityResult方法:

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String returnedData = data.getStringExtra("data_return");
                    Log.d("FirstActivity", "从上个页面返回的值" + returnedData);
                }
                break;
            default:
        }
    }

这样我们就处理好了,带值返回的问题了。具体效果可以自己去尝试。我这里由于电脑问题,就不运行虚拟机了。

再补充一下,有时候我们是根据android的虚拟键back键返回的,那么我只需要重写onBackPressed方法即可,里面的内容跟button2的点击事件内容是一样的

@Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("data_return","return value");
        setResult(RESULT_OK,intent);
        finish();
    }

由于前一段时间的需要,Flutter暂时不做了,做了一部分时间的Vue项目,然后又根据需要需要重现用原生来实现部分功能,当然我也是从Flutter刚转回原生需要重新学,如果有不对的地方希望大佬们给指正一下。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值