关于不同activity的跳转和数据传递

 acitivity之间的活动由Intent作为桥梁进行。不论是数据传递还是最基本的跳转,Intent都作为最基本媒介。

首先是最简单的跳转活动

val intent=Intent(this,ChildActivity::class.java)

startActivity(intent)

 Intent中的括号前者作为当前界面,后者作为目标界面,通过方法startActivity来实现最基础的跳转功能。值得注意的是如果在AndroidManifest里面不同的Activity没有成功注册的话,这个最基本的跳转功能是无法实现的。尤其是不同主题的继承问题,在代码报错的时候通过查看日志来排除bug.

然后是不同界面的之间的数据传输。

从父页面到子页面的数据传输较为简单,主要的两种方式是bundle和直接在Intent里面添加。

val intent =Intent(this , ChildActivity::calss.java )
val bundle=Bundle()

bundle.putString("key","value")
intent.putExtras(bundle)

startActivity(bundle)

bundle和Intent的底层是Map所以需要输入键和值两种变量。

在子页面的获取方法如下

val receive = getIntent().getExtras()
val theReceiveMessage= receive?.getString("key").toString()

这样便获得了上一页面的数据并且转换为String,接下来可以在代码里面动态设置组件里的文本

下面展示直接使用Intent传输数据的方法。

//父页面
val intent = Intent(this.ChildActivity::class.java)
intent.putExtra("key","value")
startActivity(intent)
//子页面
val receive = getIntent().getStringExtra("key").toString()

然后是子页面向父页面数据传递

// 父页面
class MainActivity : ComponentActivity() {
//注册启动qi
    private val resultLauncher=registerForActivityResult(StartActivityForResult()){result->

/*在子页面会返回result,result里面包含resultCode和data两个返回数据,resultCode通常与Activity.RESULT_OK比较来判断是否返回成功。data则是返回的数据主要是双列集合,有通过bundle和直接用Intent的两种方式,下面展示的是直接用Intent的方式*/
       
if (result.resultCode== Activity.RESULT_OK){
            val data =result.data

            val returnedData =data?.getStringExtra("the message")

            val theReturnedText=findViewById<TextView>(R.id.tv_returned)
            theReturnedText.text=returnedData.toString()

        }

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)

        val btn_turn=findViewById<Button>(R.id.btn_turn)
        btn_turn.setOnClickListener {
            val it_toReceive= Intent(this, ReceiveActivity::class.java)

            val bnd_send= Bundle()
            bnd_send.putString("the message","you will never walk alone")

            it_toReceive.putExtras(bnd_send)
//启动launcher
            resultLauncher.launch(it_toReceive)
        }
    }




//子页面

        btn_back.setOnClickListener {

            val it_return= Intent()
//在返回的Intent里面添加数据
            it_return.putExtra("the message","the forever liverpool legend 8,Steven Gerrard")
//调用setResult方法返回数据
            setResult(Activity.RESULT_OK,it_return)

            finish()
        }

        }

另一种为使用bundle

 
//父页面
private val resultLauncher=registerForActivityResult(StartActivityForResult()){result->
        if (result.resultCode== Activity.RESULT_OK){
            val data =result.data
     //获取数据后直接用而extras解析bundle

            val bnd_returned =data?.extras
            val returnedString=bnd_returned?.getString("the message")

            val theReturnedText=findViewById<TextView>(R.id.tv_returned)
            theReturnedText.text=returnedString.toString()

        }

    }





//子页面

//用bundle添加数据
btn_back.setOnClickListener {

            val it_return= Intent()

            val bnd_returned= Bundle()
            bnd_returned.putString("the message","the forever liverpool legend 8,Steven Gerrard")
            
            it_return.putExtras(bnd_returned)

            setResult(Activity.RESULT_OK,it_return)

            finish()
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值