During the Android development process, we may store some global variables, preferably in any activity or service in the app, and we can use application.
One of our applications is called application, then it should be very good to understand that there will only be a single application in a singleton application, it is not difficult to think of this in the storage of global variables, then how exactly is stored?
First, we create a application that inherits Android.app.Application:
<span style= "FONT-SIZE:18PX;" >package Com.podongfeng.firstapplication.app;import Android.app.application;public class MyApplication extends Application {Private integer allviewinteger;public integer getallviewinteger () {return allviewinteger;} public void Setallviewinteger (Integer allviewinteger) {this.allviewinteger = Allviewinteger;}} </span>
Then, in Androidmanifest.xml to declare the application, a bit similar to declaring activity.
In fact, in Androidmanifest.xml there will be a system declaration of application, similar to this:
<span style= "FONT-SIZE:18PX;" ><application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ". Splashactivity " android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name= ". Mainactivity " android:label=" @string/app_name "> </activity> </application></ Span>
So, how do we replace ourselves with application?
In fact, just add the Android:name attribute to our custom application in the application tag:
<span style= "FONT-SIZE:18PX;" ><application android:name= "Com.podongfeng.firstapplication.app.MyApplication" android: Allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android: Theme= "@style/apptheme" > <activity android:name= ". Mainactivity " android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <activity android:name= ". Secactivity " android:label=" @string/app_name "> </activity> </application></ Span>
OK, so we can use Getapplicationcontext () in the activity to get this custom application.
Wait, is not the Bureau of this is not particularly convenient, if you write some common Java method, in order to good code reuse, not put in the activity inside it?
Pass the context through a parameter and then use context to get application?
This is certainly possible, however, since application is a singleton, we would like to think of using the GetInstance method in a singleton design pattern to get the object in a single case. In fact, our MyApplication integrates application, which can be directly overwrite the OnCreate method, assigning the object to a static member variable when application is created, so that It is possible to obtain this singleton by MyApplication static method anywhere:
<span style= "FONT-SIZE:18PX;" >package Com.podongfeng.firstapplication.app;import Android.app.application;public class MyApplication extends Application {private static MyApplication MyApplication = Null;public static MyApplication getmyapplication () {return MyA pplication;} Private integer allviewinteger;public integer getallviewinteger () {return allviewinteger;} public void Setallviewinteger (Integer allviewinteger) {this.allviewinteger = Allviewinteger;} @Overridepublic void OnCreate () {super.oncreate (); myapplication = this;}} </span>
OK, we currently only write a usable global variable Allviewinteger in it, this is only to illustrate the problem is enough, what to save what to save, it is very convenient to obtain, finally attached to the 2 activity in the set and get a global variable sample:
<span style= "FONT-SIZE:18PX;" >package Com.podongfeng.firstapplication;import Com.podongfeng.firstapplication.app.myapplication;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.button;public class Mainactivity extends Activity implements Onclicklistener {private Button nextbtn;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); MyApplication MyApplication = Myapplication.getmyapplication (); Myapplication.setallviewinteger (+); nextBtn = ( Button) Findviewbyid (R.id.btn_next); Nextbtn.setonclicklistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic Boolean OnoptionsitemseLected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} @Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setclass (Getapplicationcontext (), Secactivity.class); startactivity (intent);}} </span>
<span style= "FONT-SIZE:18PX;" >package Com.podongfeng.firstapplication;import Com.podongfeng.firstapplication.app.myapplication;import Android.app.activity;import Android.os.bundle;import Android.widget.textview;public class SecActivity extends Activity {private TextView TextView = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activiry_sec); TextView = (TextView) Findviewbyid (R.ID.TV_SEC); Textview.settext (String.valueof (Myapplication.getmyapplication (). Getallviewinteger ()));}} </span>
Using Application in Android