Uncle also said Xamarin ~ Android ~ An array is transmitted between activities. xamarinandroid

Source: Internet
Author: User

Uncle also said Xamarin ~ Android ~ An array is transmitted between activities. xamarinandroid

When developing an application, we cannot use only one Layout or Activity. For example, if you have a management system that requires users to log in and use it again, you must have at least two activities, log on to another Activity first, and then jump to another Activity. This problem occurs when data is transmitted between multiple activities. This is what we want to talk about today!

First talk about Activity

First, an Activity will inherit the Activity class. There are methods in this class that have been implemented for us. We can regard activity as an Action in MVC, that is, it is responsible for rendering our page, the data required on the component page. Let's take a look at several events (methods) that an Activity experiences when rendering a page ), we can rewrite these methods in a custom activity!

OnCreate: Create an interface to initialize data;

OnStart: In this step, it becomes "visible to users and interactive;

OnResume: Can interact with users. (in the Activity stack system, these activities are managed through stacks, that is, the current Activity is at the top of the stack. After the stack is run, a pop-up stack is displayed, return to the previous Activity );

OnPause: This step is visible but interactive, and the system will stop animation and other CPU-consuming tasks. We know from the above description that some of your data should be stored here, because your program's priority is reduced and may be reclaimed by the system. The data stored here should be read in onResume.

OnStop: Becomes invisible and overwritten by the next activity

OnDestroy: This is the last method called before the Activity is killed. It may be that other classes call the finish method or the system temporarily kills the method to save space. You can use isFinishing () to judge it, if you have a Progress Dialog thread running, delete the cancel in onDestroy. Otherwise, when the thread ends, the cancel Method of Dialog will throw an exception.

In onPause, onstop, and onDestroy states, the activity may be killed by the system.

Let's talk about data transmission between activities.
/// <Summary> /// the event after the item is clicked /// </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> void listView_ItemClick (object sender, adapterView. itemClickEventArgs e) {Toast. makeText (this, "you selected" + datas [e. position]. title, ToastLength. short ). show (); Intent intent = new Intent (this, typeof (UserInfoLayoutActivity);/* store the data to be transferred through the Bundle object */Bundle bundle = new Bundle (); /* character, String, Boolean, byte array, floating point number, etc., can be passed */intent. putExtra ("Title", datas [e. position]. title); intent. putExtra ("Desc", datas [e. position]. desc); intent. putExtra ("AssistsCount", datas [e. position]. assistsCount); intent. putExtra ("Fails", datas [e. position]. fails); intent. putExtra ("Score", datas [e. position]. score); intent. putExtra ("Level", datas [e. position]. level); intent. putExtra ("Image", datas [e. position]. image);/* assign the bundle object assign to Intent */intent. putExtras (bundle); StartActivity (intent );}

The above code is the event triggered after a project is clicked, and an Intent object is created for the event, which is the basis for cross-Activty value passing, and then PutExtra assigns values to them, it is equivalent to a hash table. OK. Let's take a look at how to accept the data on the second page.

 

Protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. userInfoLayout); // specify the view to be rendered var listView = FindViewById <ListView> (Resource. id. userInfoViewMain); listView. adapter = new UserAdapter (this, new UserInfo {Title = Intent. getStringExtra ("Title"), Desc = Intent. getStringExtra ("Desc"), scount = Intent. getIntExtra ("scount", 0), Level = Intent. getIntExtra ("Level", 0), Fails = Intent. getIntExtra ("Fails", 0), Image = Intent. getIntExtra ("Image", 0), Score = Intent. getIntExtra ("Score", 0 ),});}

 

In the code, OnCreate is the method of each Activity, that is, it is the first method to be executed after it enters the Activity. Here it is generally used to create a view, initialize data, load PartialView, and finally, let's take a look at the final effect of uncle's program execution.

Second page

Haha, how is it interesting!

 

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.