android Intent 常见用法和使用场景

Intent 的意思就是意图的意思就是代表我们所要执行的操作,我们可以通过Intent的来完成activity之间的数据关联和跳转问题。

下面是Intent经常使用的场景和用法、

1.Activity 之间的简单跳转

Intent intent = new Intent ();
intent.setClass(FristActivity.this,SecondActivity.class);
startActivity(intent);

2.Activity 之间的数据传递

<span style="font-size:18px;">Intent intent = new Intent();
intent.setClass(FristActivity.this,SecondActivity.class);
Bundle bundle = new Bundle ();
bundle.putString ("String","shuju");
intent.putExtras(bundle);
startActivity(intent);</span>
数据的接收

Bundle bundle = getIntent().getExtras();
String date = bundle.getString("String");
3.向上一个activity返回数据

适应场景,消息阅读后,原来的消息提示框变色,

//触发下一个activity的activity
Intent intent = new Intent();
intent.setClass (MainActivity.this,LoginActivity.class);
startActivityForResult(intent,REQUEST_CODE);
//向上一个activity返回数据
Intent intent=getIntent();
        Bundle bundle2=new Bundle();
        bundle2.putString("name", "This is from ShowMsg!");
        intent.putExtras(bundle2);
        setResult(RESULT_OK, intent);

4.回调方法
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_CODE){
            if(resultCode==RESULT_CANCELED)
                  setTitle("cancle");
            else if (resultCode==RESULT_OK) {
                 String temp=null;
                 Bundle bundle=data.getExtras();
                 if(bundle!=null)   temp=bundle.getString("name");
                 setTitle(temp);
            }
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值