Data transfer between Acticity in Android

Source: Internet
Author: User

Data transfer between Acticity in Android
During Android development, it is very common to transmit data between different acitifiers. I spent a little time summarizing and recording the data transfer between Acitivity. 1. simple transfer of key-value pairs this transfer method is very simple, you only need to add the corresponding key-value pair in the Intent construction. In ActivityA, the code for calling Intent is as follows: 1 Intent I = new Intent (ActivityA. this, ActivityB. class); 2 I. putExtra ("name", "Finlay Liu"); 3 I. putExtra ("age", "19"); 5 startActivity (I); In ActivityB, you can directly read the corresponding key-value pairs. 1 String s = getIntent (). getStringExtra ("name") + ":" + getIntent (). getStringExtra ("age"); 4 Toast. makeText (this, s, Toast. LENGTH_SHORT ). show (); 2. It is also very common to transfer objects between different acitifiers. I did not carefully read the Android development documentation before, so when I was writing Android code, object transmission between different acitifiers was implemented through static classes. It is also very simple to transfer objects between activities at the beginning, similar to the above key-Value Pair Transfer Method. First, add the serialization interface to the class to be passed: 1 package com. finlayliu. passingobject; 2 3 import java. io. serializable; 4 5 public class Person implements Serializable {6 7 private static final long serialVersionUID = 1L; 8 9 public int getId () {10 return id; 11} 12 13 public void setId (int id) {14 this. id = id; 15} 16 17 public String getName () {18 return name; 19} 20 21 public void setName (String name) {22 this. name = name; 23} 24 25 public int getAge () {26 return age; 27} 28 29 public void setAge (short age) {30 this. age = age; 31} 32 33 private int id; 34 private String name; 35 private int age; 36 37 public Person () {38 39} 40 41 public Person (int id, string name, int age) {42 this. id = id; 43 this. name = name; 44 this. age = age; 45 46} 47 48 public String toString () {49 return id + ":" + this. name + ":" + age; 50} 51} copy the code in ActivityA. The code for calling Intent is as follows: 1 Intent I = new Intent (MainActivity. this, OtherActivity. class); 2 Person p = new Person (1, "Finaly Liu", 19); 3 I. putExtra ("Person", p); 4 5 startActivity (I); In ActivityB, read the corresponding object code as follows: 1 Person p = (Person) getIntent (). getSerializableExtra ("Person"); 2 Toast. makeText (getApplicationContext (), p. toString (), Toast. LENGTH_LONG ). show ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.