您的位置:首页 > 其它

实现一个activity中传输信息到另一个activity

2017-03-19 10:45 176 查看
    android开发中经常需要处理各种各样不同的信息,而不同activity中发送和接收信息对于开发也是相当重要的。

方法一:
        发送方代码:
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);                        Bundle bundle=new Bundle(); 
bundle.putInt("position", position);
intent.putExtras(bundle);
startActivity(intent);

        接收方代码:

        Bundle bundle = getIntent().getExtras();//获取从mainactivity中传过来的对象
int position = bundle.getInt("position");//根据设定的键值获取对应的信息
方法二:
        发送方:

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("position", position);
intent.putExtra("name", name);
startActivity(intent);
        接收方:

        Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);
String name = intent.getStringExtra("name");
        

接收到的内容就可以直接利用
本文出自 “11828641” 博客,请务必保留此出处http://11838641.blog.51cto.com/11828641/1842683
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐