In Android projects, some global static variables are sometimes required to save some data. In this way, other pages can call the data after the value assignment interface is disabled.
But we know that global static variables in Java (Java does not have the concept of global variables, but Java provides the public static keyword to implement some keywords similar to global variables) they are all stored in the memory when the program is loaded. They are stored in the method area. If the program does not end, it will always exist. This will affect the system performance. In Android, Can I pass the value in this way?
Today I made a small demo, and it feels pretty good: data is transmitted between two activities without using Global static variables.
A sendactivity (the party that saves the data or the party that transmits the data) and a receiveractivity (the party that uses the data) have only one button in the layout file corresponding to each activity for simplicity.
Java code of sendactivity
Public class sendactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_send); // obtain the button object: button btnsendbutton = (button) findviewbyid (R. id. btnsend); btnsendbutton. setonclicklistener (New onclicklistener () {public void onclick (view v) {intent = new intent (); // The first parameter is the context of the application, the lifecycle is the entire application // The second parameter is the full path intent of the page to be redirected. setclassname (getapplicationcontext (), "com. example. testofdialog. receiveactivity "); // The bundle class is used to carry data. It is similar to map and is used to store value bundle B = new bundle (); B. putstring ("I am the key", "Here is the content you want to pass"); // here putextras is used, and the receiver uses getextra intent for response. putextras (B); startactivity (intent); // close the current page system. exit (0); // both exit and finish are supported. // sendactivity. this. finish ();}});}}
Java code of receiveactivity
Public class receiveactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_receive); button btnreceivebutton = (button) findviewbyid (R. id. btnreceive); btnreceivebutton. setonclicklistener (New onclicklistener () {public void onclick (view v) {// The final parameter must be the same as that of the sender. Otherwise, a null value string rstring = getintent () is obtained (). getextras (). getstring ("I Am a key"); toast. maketext (receiveactivity. this, rstring, toast. length_short ). show ();}});}}
Here we only introduce the values transmitted between two activities through intent. If three activities are displayed in sequence, the values in the first activity must be used for the third activity, is this method still effective? Are there other better methods?
I have not conducted any experiments yet, and I don't know. I hope you can try it on your own. I will continue to try it and will give answers later.