When we develop the application, it is not possible to use only one layout or an activity, such as your management system, require users to log in and then use, then you have at least two activity bar, first landing one, and then successful need to jump to the other activity, This is the problem of passing data between multiple activity, which is what we are going to say today!
Let's talk about activity.
The first activity is to inherit the activity class, which has methods that have been implemented for us, and we can think of activity as an action in MVC, which is responsible for rendering our pages, the data needed on the component pages, Let's take a look at a few events (methods) that the activity will go through when rendering the page, and we can all rewrite it in our custom activity!
onCreate: Create the interface here, do some data initialization work;
OnStart: To this step becomes "user visible non-interactive" state;
Onresume: Become and user-interactive, (in the activity stack system through the stack of the activity, that is, the current activity at the top of the stack, run out of pop-up stack, then back to the previous activity);
OnPause: To this step is visible but not interactive, the system will stop the animation and other CPU-consuming things. As you know from the above description, you should save some of your data here, because your program's priority is reduced and may be withdrawn by the system. The data that is stored here should be read in the Onresume.
onStop: becomes invisible and is covered by the next activity
OnDestroy: This is the last time the activity was killed before the method is called, perhaps other classes call the Finish method or the system in order to save space to temporarily kill it, you can use isfinishing () to judge it, if you have A progress dialog is running in the thread, please cancel it in OnDestroy, or else the Cancel method called dialog will throw an exception when it ends.
Onpause,onstop, OnDestroy, in three different states, the activity can be killed by the system.
Besides, the data transfer between the activity
/// <summary> ///Click the event after the item is selected/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> voidListview_itemclick (Objectsender, Adapterview.itemclickeventargs e) {Toast.maketext ( This,"you chose."+Datas[e.position]. Title, Toastlength.short). Show (); Intent Intent=NewIntent ( This,typeof(userinfolayoutactivity)); /*storing data that needs to be passed through the bundle object*/Bundle Bundle=NewBundle (); /*characters, strings, Booleans, byte arrays, floating-point numbers, and so on, 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 to intent*/Intent. Putextras (bundle); StartActivity (Intent); }
The above code is the event that is triggered after a project has been clicked, and the event establishes the intent object, which is the basis of the cross-activty value, and then PutExtra assigns them a value, which is equivalent to a hash table, OK, Let's take a look at the second page on how to accept the data .
protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.UserInfoLayout);//Specify the view you want to render varListView = findviewbyid<listview>(Resource.Id.userInfoViewMain); Listview.adapter=NewUseradapter ( This,NewUserInfo {Title= Intent.getstringextra ("Title"), Desc= Intent.getstringextra ("Desc"), Assistscount= Intent.getintextra ("Assistscount",0), level= Intent.getintextra (" Level",0), fails= Intent.getintextra ("fails",0), Image= Intent.getintextra ("Image",0), score= Intent.getintextra ("score",0), }); }
The OnCreate in the code is the method that every activity has, that is, it is the first method to execute after entering activity, here is generally to build the view, initialize the data, load the Partialview, finally, we look at the final effect of the program execution of the uncle.
A second page
Oh, how, very interesting!
Uncle also said Xamarin~android ~activity between the passing array