如何从一个活动(意图)向另一个活动发送数据?
我使用以下代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);如何从一个活动(意图)向另一个活动发送数据?
我使用以下代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);Answers:
首先,使用以下getIntent()方法获取启动活动的意图:
Intent intent = getIntent();如果多余的数据用字符串表示,则可以使用intent.getStringExtra(String name)method。在您的情况下:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");getIntent().getStringExtra("id");以获取ID字符串
                    getIntent()method 获得启动活动的意图。我已经更新了答案。
                    在接收活动中
Bundle extras = getIntent().getExtras(); 
String userName;
if (extras != null) {
    userName = extras.getString("name");
    // and get whatever type user account id is
}getStringExtra?
                    null,则可以跳过整个Extras获取。通过使用getStringExtra,您基本上将其更改为一系列if(extras != null) { return extras.getString(name) }。每个getStringExtra呼叫一个。该选项将检查null一次,如果是的话,则根本不会去阅读它Bundle。除此之外,每次getStringExtra都可能还会继续在getExtras内部进行询问。因此,您只需调用更多函数即可。
                    //  How to send value using intent from one class to another class
//  class A(which will send data)
    Intent theIntent = new Intent(this, B.class);
    theIntent.putExtra("name", john);
    startActivity(theIntent);
//  How to get these values in another class
//  Class B
    Intent i= getIntent();
    i.getStringExtra("name");
//  if you log here i than you will get the value of i i.e. johnString value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    value = bundle.getString("sample_name");
}如果在FragmentActivity中使用,请尝试以下操作:
第一页扩展了FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);在片段中,您只需getActivity()要先调用,
第二页扩展了Fragment:
String receive = getActivity().getIntent().getExtras().getString("name");如果您试图获取碎片中的额外数据,则可以尝试使用:
使用以下方式放置数据:
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);使用以下方法获取数据:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  getArguments().getInt(ARG_SECTION_NUMBER);
  getArguments().getString(ARG_SECTION_STRING);
  getArguments().getBoolean(ARG_SECTION_BOOL);
  getArguments().getChar(ARG_SECTION_CHAR);
  getArguments().getByte(ARG_SECTION_DATA);
}您可以从intent中获取任何类型的额外数据,无论它是对象,字符串还是任何类型的数据。
Bundle extra = getIntent().getExtras();
if (extra != null){
    String str1 = (String) extra.get("obj"); // get a object
    String str2 =  extra.getString("string"); //get a string
}和最短溶液是:
Boolean isGranted = getIntent().getBooleanExtra("tag", false);**按意图放入数据-**
 Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
            intent.putExtra("subjectName","Maths");
            intent.putExtra("instituteId",22);
            mContext.startActivity(intent);**通过意图获取数据-**
  String subName = getIntent().getStringExtra("subjectName");
  int insId  getIntent().getIntExtra("instituteId",0);如果我们通过Intent 发送Integer值。当我们必须在getIntent()。getIntExtra(“ instituteId”,0)中使用第二个参数0时, 否则我们不使用0,那么android会给我错误。
我们可以通过简单的方法做到这一点:
在FirstActivity中:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);在SecondActivity中:
    try {
        Intent intent = getIntent();
        String uid = intent.getStringExtra("uid");
        String pwd = intent.getStringExtra("pwd");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("getStringExtra_EX", e + "");
    }在“第一个活动”中传递具有价值的意图:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);收到第二项活动的意向;-
Intent intent = getIntent();
String user = intent.getStringExtra("uid");
String pass = intent.getStringExtra("pwd");我们通常使用两种方法来发送值和获取值。对于发送的值,我们将使用intent.putExtra("key", Value);和接受意向在另一个活动中,我们将使用intent.getStringExtra("key");获得的意图数据,String或使用一些其他可用的方法来获得其他类型的数据(的Integer,Boolean等等)。键可以是任何关键字来标识值,表示您正在共享什么值。希望它对您有用。
您也可以这样
//将价值放入意图中
    Intent in = new Intent(MainActivity.this, Booked.class);
    in.putExtra("filter", "Booked");
    startActivity(in);//从意图中获取价值
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String filter = bundle.getString("filter");要从Intent访问数据,您应该知道两件事。
Intent类中有不同的方法可以提取不同类型的数据类型。看起来像这样
getIntent()。XXXX(KEY)或intent.XXX(KEY);
因此,如果您知道在otherActivity中设置的变量的数据类型,则可以使用相应的方法。      
String profileName = getIntent().getStringExtra("SomeKey");您可以在Intent的官方文档中查看可用方法的列表。
这是用于适配器的,对于活动,您只需将mContext更改为您的Activty名称,对于片段,您需要将mContext更改为getActivity()
 public static ArrayList<String> tags_array ;// static array list if you want to pass array data
      public void sendDataBundle(){
            tags_array = new ArrayList();
            tags_array.add("hashtag");//few array data
            tags_array.add("selling");
            tags_array.add("cityname");
            tags_array.add("more");
            tags_array.add("mobile");
            tags_array.add("android");
            tags_array.add("dress");
            Intent su = new Intent(mContext, ViewItemActivity.class);
            Bundle bun1 = new Bundle();
            bun1.putString("product_title","My Product Titile");
            bun1.putString("product_description", "My Product Discription");
            bun1.putString("category", "Product Category");
            bun1.putStringArrayList("hashtag", tags_array);//to pass array list 
            su.putExtras(bun1);
            mContext.startActivity(su);
        }我刚刚在这里发布了一个涵盖该主题的答案,其中包括一些替代方法。
它利用了Vapor API,这是我编写的简化jQuery开发的新jQuery启发式Android框架。查看该答案中的示例,了解如何轻松在活动之间传递数据。
它还演示了VaporIntent,它使您可以链接方法调用并利用重载的.put(...)方法:
$.Intent().put("data", "myData").put("more", 568)...您可以使用Vapor API轻松地在整个应用程序中传递数据,因此希望对您和其他人在应用程序开发过程中有所帮助。
user.getUserAccountId()+""中将int转换为字符串的一种好方法,因为这会创建不必要的对象被收集。考虑使用String.valueOf(user.getUserAccountId)或Integer.toString(user.getUserAccountId)代替。