This article shows you how Android uses application objects to access public data.
When running each application, the Android system creates a application object that stores the public variables associated with the entire application. An Android app will generate only one application object, and the Application object in the unused activity is the same, so the Application object is a singleton (SingleTon). The Application object is ideal for storing data related to the entire application, such as app version, app login account, data cache, etc.
Use application objects to store public data or data passing
In Android development, activity switching is very frequent, almost as much as switching between different pages in a Web site. There is a need to store public information (such as only one currently logged in) and data transfer between different activity. The following is a way to use the Application object to store logged-in user information, which can be found to be convenient for different activity to obtain login user information.
First, create a new Java class to inherit the application class: Myapplication.java
Package Com.example.applicationtest;import android.app.Application; Public classMyApplication extends Application { PublicString appversion ="v1.0"; //currently logged in user PrivateUser Loginuser =NewUser (); PublicUser Getloginuser () {returnLoginuser;} Public voiduserlogin (user user) {Loginuser.setuserid (User.getuserid ()); Loginuser.setusername (User.getusername ()); } Public voiduserlogout () {Loginuser=NewUser ();}}
Specify the Application object for the app in Androidmanifest.xml
<?xml version="1.0"encoding="Utf-8"? ><manifest xmlns:android="http://schemas.android.com/apk/res/android" Package="com.example.applicationtest"Android:versioncode="1"Android:versionname="1.0"> <uses-SDK Android:minsdkversion="8"android:targetsdkversion=" -"/> <Application Android:name="com.example.applicationtest.MyApplication"Android:allowbackup="true"Android:icon="@drawable/ic_launcher"Android:label="@string/app_name"Android:theme="@style/apptheme"> <Activity Android:name="com.example.applicationtest.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> </application></manifest>
Use the Application object in activity, using the activity's Getapplication () method.
Package com.example.applicationtest;import Android.os.bundle;import android.app.Activity; Public classMainactivity extends Activity {PrivateMyApplication mapplication; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); //get the Application object for the entire app//The objects obtained in different activity are the sameMapplication =(MyApplication) getapplication (); } /** * Generally only in the login interface to set the login user information, in other activity * as long as through the Application object can get login user information*/ Private voidLogin () {User User=NewUser (); User.setuserid (1); User.setusername ("Raysmond"); //save logged-in user information to the Application objectmapplication.userlogin (user);}}
It can be found that data sharing can be conveniently implemented between different activity through application objects. This is much more convenient than passing data through bundles each time you switch activity.
The traditional way of using bundles to pass data before activity
Suppose we have two activity:activity1 and activity2,activity1 switch to Activity2 and pass the user information.
Activity1.java
Package Com.example.applicationtest;import Android.os.bundle;import android.app.activity;import android.content.Intent; Public classActivity1 extends Activity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_activity1); //Switch to Activity2gotoActivity2 ();} Private voidGotoActivity2 () {Intent Intent=NewIntent (Activity1. This, Activity2.class); Bundle Bundle=NewBundle (); Bundle.putlong ("user_id",1); Bundle.putstring ("user_name","Raysmond"); Intent.putextras (bundle); StartActivity (Intent); }}
Activity2.java
Package Com.example.applicationtest;import Android.os.bundle;import android.app.activity;import android.content.Intent; Public classActivity2 extends Activity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_activity2); GetUserInfo (); } /** * Get the data passed from the previous activity*/ Private voidGetUserInfo () {Intent Intent= This. Getintent (); Bundle Bundle=Intent.getextras (); LongUserId = Bundle.getlong ("user_id", -1L); String UserName= Bundle.getstring ("user_name"); }}
We can find that this method of transmitting data is cumbersome, especially when the activity is numerous, switching very frequently. The use of application objects is more concise and convenient when the entire application's public data (such as login information) is the same between different activity and needs to be used.
Android uses application objects to access public data