Application of application in wirelessly

Source: Internet
Author: User

I learned it yesterday. Using volley to operate the network via get and post, it mentions a class called application, which has his own life cycle, very similar to the application of the Web, from the beginning of life to the end of life, is a global session, the scope is very wide, and Android also provides a default call, we usually do not have to him or the tube. When I studied volley, he was mainly to create volley object, give him a global. Later, I thought, since it is the overall situation, then we can store some of our public data in it, lest we write the same method, and parameters. With this question, I started my operation.

First of all. We customize a application class for storing our data

Package Com.example.aplicationdemo;import Java.util.hashmap;import Java.util.map;import android.app.Application; public class MyApplication extends application {private static map<string, object> Map; @Overridepublic void Oncreat E () {//TODO auto-generated method Stubsuper.oncreate (); map = new hashmap<string, object> ();} public static map<string, object> Getmap () {return Map;}}

As for the data stored in that activity, depending on the situation, I put it in the mainactivity for demonstration.

Package Com.example.aplicationdemo;import Java.util.map;import Android.app.activity;import android.content.Intent; Import Android.os.bundle;import android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.edittext;import Android.widget.imageview;public Class Mainactivity extends Activity {private EditText edittext;private ImageView img; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); EditText = ( EditText) Findviewbyid (r.id.txt); img = (ImageView) Findviewbyid (r.id.img); Findviewbyid (R.ID.BTN). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {String edstring = Edittext.gettext (). ToString (). Trim (); int imgs = R.DRAWABLE.TG; map<string, object> map = Myapplication.getmap (), Map.put ("TXT", edstring), Map.put ("img", IMGs); Intent Intent = New Intent (Getapplicationcontext (), my.class); StartActivity (Intent);}});}} 

In order to experience the effect better, I get the data in another page

Package Com.example.aplicationdemo;import Java.util.collection;import Java.util.iterator;import java.util.Map; Import Java.util.set;import android.app.activity;import Android.media.image;import Android.os.bundle;import Android.widget.imageview;import Android.widget.textview;public class My extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.my); TextView t = (TextView) Findviewbyid (r.id.t); ImageView img = (ImageView) Findviewbyid (R.id.ig); map<string, object> map = Myapplication.getmap ();//Gets the key of the map set<string> set = Map.keyset (); if (set! = null) {It Erator<string> it = Set.iterator (), while (It.hasnext ()) {String st = It.next ();//Match his key to avoid data matching exception if (st.equals ("TXT")) {Object value = Map.get (ST); T.settext (value.tostring ());} else if (st.equals ("img")) {Object value = Map.get (ST); int i = (int) value;img.setimageresource (i);}}}}
This is my study note.

Application of application in wirelessly

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.