首页 > 代码库 > 实现一个activity中传输信息到另一个activity

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

    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

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