Android programming enables the sharing of data between two activity and the methods of accessing each other _android

Source: Internet
Author: User

The example in this article describes how Android programming enables you to share data between two activity and access each other. Share to everyone for your reference, specific as follows:

I've turned from Windows programming to learning about Android, and I've been thinking that if two of the activity can be like a C # or a form in Delphi, you have direct access to its members (characters, numbers, member objects, and so on), and you can call its exposed methods, That should be more convenient than using intent to transmit data, and then try the following methods, the test is basically no problem, sent to everyone to discuss. I learn Android soon, naïve place hope everybody don't laugh

Principle: Suppose there are two Activity:activitymain and activitynew, start activitynew in Activitymain, and in activitynew you can access the member objects in the Activitymain. Invokes the method that it exposes. Then define a static member variable in Activitymain, type Activitymain, and then in the OnCreate process, assign the Activitymain instance this to the member:

public class Activitymain extends activity {public
 static activitymain mainact;//define a member variable of this type
 String getstr () { return
  "Shao Jian";
 }
 @Override public
 void OnCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.main); 
  Mainact = this; Here, assign this instance to a static member variable
 }
...
}

To access the member variables and methods in Activitymain in Activitynew:

public class Activitynew extends activity {
 @Override
 protected void onCreate (Bundle savedinstancestate) {
  String title = bundle.getstring ("title");
  title = MainActicivity.MainAct.GetStr (); Invoke member Method
  T.settext (Title.trim ());
  Super.oncreate (savedinstancestate);
 }


I hope this article will help you with the Android program.

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.