Android入门之路 - 多级Activity进行数据传递

因为我是在写监听网络的时候,不经意想起多级Activity是否可以数据传递(当然再此之前我一般都只是进行A-B的数据传递),因是临时想起,所以包名,XMl名,VIew id并不是很规范。

近些年

gogogo ~

Activity 传值基础

Activity之间传值的场景比较多见,也是入门的基础,在此重做记录

回头看,俩种方式的区别

  • Intent旨在数据传递bundle旨在存取数据,当然intent也提供一部分数据的存取,但比起bundle就显得不专业,不灵活的多
  • Intent适合传递简单数据,bundle适合传递对象数据
  • intent传值内部代码也是使用了bundle
    在这里插入图片描述
传递方式
Intent

其实intent可以传递很多类型的值,提前知悉

在这里插入图片描述

我比较喜欢这种方式,因为懒,可以少写几行代码...

  • 传递
 Intent intent = new Intent(this, BActivity.class);
 intent.putExtra("name","大帅哥");
 startActivity(intent);
  • 接收
 Intent intent = getIntent();
 String name = intent .getStringExtra("name")
Intent + Bundle
  • 传递
 Intent intent= new Intent(MainActivity.this,BActivity.class);
 Bundle b=new Bundle();
 b.putString("name","大帅哥");
 b.putInt("age",21);
 intent.putExtras(b);
 startActivity(intent);
  • 接收
 Intent intent = getIntent();
 Bundle data = intent.getExtras();
 String name = data.getString("name");
 Int age = data.getInt("age");
通用方式

传递 - (我是直接用的Intent,主要图方便)

 Intent sendIntent = new Intent();
 sendIntent.putExtra("key",1);
 startActivity(sendIntent);

需要注意: sendIntent.putExtra("key", 可随意传各种类型数据,内部会自动转类型);

传递对象、实体类时记得进行序列化

在这里插入图片描述

接收

Intent方式

 Intent receiveIntent  = getIntent();
 int key = receiveIntent.getIntExtra("key", 0);

接收数据Api

  • intent.get类型Extra("key")
  • intent.get类型Extra("key",需要提供一个默认值)

示例

获取Int传值

在这里插入图片描述
获取String传值

在这里插入图片描述

Bundle 方式

 Intent receiveIntent  = getIntent();
 Bundle extras = receiveIntent.getExtras();
 int key = extras.getInt("key");

可直接get对应类型,省得后面转类型

在这里插入图片描述


多级Activity传递数据

仅作为一种场景进行记录

效果

执行流程

这里写图片描述
运行结果

这里写图片描述

使用方式

Tip:

  • AndroidMainfest注册Activity
 <activity android:name=".SecoundActivity"/>
 <activity android:name=".ThirdActivity"/>
  • 代码中的NetState不用处理
  • Xml中的隐藏View不用处理

MainActivity(一级)

package com.example.dow.receiverdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView mState;
    private TextView mBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        mState = (TextView) findViewById(R.id.net_state);
        mBtn = (TextView) findViewById(R.id.mBtn_first);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	//传递数据
                Intent intent=new Intent(MainActivity.this,SecoundActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("data","First数据传递");
                intent.putExtras(bundle);
                Log.e("tag","被点击");
                startActivity(intent);
            }
        });
    }
}

SecoundActivity(二级)

package com.example.dow.receiverdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

/**
 * Created by YongLiu on 2017/5/24.
 */

public class SecoundActivity extends AppCompatActivity {

    private TextView mBtn;
    private String data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secound);
		
		//获取intent传递数据
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        data = (String) bundle.get("data");
        Log.e("tag","MainFirst传输过来的数据===="+ data);

        mBtn = (TextView) findViewById(R.id.mBtn_secound);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	//继续传递数据
                Intent intent=new Intent(SecoundActivity.this,ThirdActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("data",data);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
 }

ThirdActivity(三级)

package com.example.dow.receiverdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

/**
 * Created by YongLiu on 2017/5/25.
 */

public class ThirdActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        
		//获取数据
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String data = (String) bundle.get("data");
        Log.e("tag","Secound传输过来的数据===="+data);
    }
}

activity_main

<?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="com.example.dow.receiverdemo.MainActivity">
    
    <TextView
        android:layout_width="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:id="@+id/net_state"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:text="隐藏的网络状态" />
        
    <TextView
        android:layout_width="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:id="@+id/mBtn_first"
        android:layout_height="wrap_content"
        android:text="事件多级传递" />
</LinearLayout>

activity_secound

<?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">
   
    <TextView
        android:layout_width="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:id="@+id/net_state_secound"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:text="隐藏的网络状态" />
        
    <TextView
        android:layout_width="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:id="@+id/mBtn_secound"
        android:layout_height="wrap_content"
        android:text="Secound事件多级传递" />
</LinearLayout>

activity_third

<?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">
    
    <TextView
        android:layout_width="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:id="@+id/net_state_three"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:text="隐藏的网络状态" />
        
    <TextView
        android:layout_width="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:id="@+id/mBtn_three"
        android:layout_height="wrap_content"
        android:text="Third事件多级传递" />
</LinearLayout>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远方那座山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值