Android uses application objects to access public data

Source: Internet
Author: User

When running each application, the Android system creates a application object that stores the public variables associated with the entire application. An Android application generates only one application object, and the Application object is the same in the unused activity, so the Application object is a single instance (SingleTon). Application objects are ideal for storing data related to the entire application, such as application versions, application login accounts, data caching, and so on.

Using application objects to store public data or data delivery
In Android development, activity switching is frequent, almost as much as switching from one Web page to another. There is a need to store public information (such as only one current logged-on user) and data transfer between different activity. The following is a way to store logged on user information using the Application object, which can be found to be very convenient for different activity to obtain login user information.

First, create a new Java class to inherit the application class: Myapplication.java

  code is as follows copy code

Package com.example.applicationtest;

Import android.app.Application;

Public class MyApplication extends application {
 
 public String appversion = "v1.0";
 
 //the current logged-on user
 private User Loginuser = new user ();
 
 
 public user Getloginuser () {
  return Loginuser;
 }
 
 public void userlogin (user user) {
  loginuser.setuserid (User.getuserid ());
  loginuser.setusername (User.getusername ());
 }
 
 public void Userlogout () {
  loginuser = new User ();
 }
}

Specify the application's Application object in Androidmanifest.xml

The code is as follows Copy Code

<?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 the activity, using the Getapplication () method of the activity.

The code is as follows Copy Code

Package com.example.applicationtest;

Import Android.os.Bundle;
Import android.app.Activity;

public class Mainactivity extends activity {

Private MyApplication mapplication;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Get the Application object for the entire application
The objects obtained in different activity are the same
Mapplication = (MyApplication) getapplication ();

}

/**
* Generally only in the login interface to set the login user information, in other activity
* Access to login user information as long as the application object is passed
*/
private void Login () {
User user = new user ();
User.setuserid (1);
User.setusername ("Raysmond");
To save the logon user information to the Application object
Mapplication.userlogin (user);
}

}

It can be found that data sharing can be easily realized between different activity through application objects. This is much more convenient than passing data through bundle each time the activity is switched.

The traditional way of using bundle to pass data before an activity
Suppose we have two activity:activity1 and activity2,activity1 switching to Activity2 and passing user information.

The code is as follows Copy Code

Activity1.java

Package com.example.applicationtest;

Import Android.os.Bundle;
Import android.app.Activity;
Import android.content.Intent;

public class Activity1 extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_activity1);
Switch to Activity2
GotoActivity2 ();
}

private void GotoActivity2 () {
Intent Intent = new Intent (activity1.this,activity2.class);
Bundle Bundle = new Bundle ();
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 class Activity2 extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_activity2);
GetUserInfo ();

}

/**
* Get the data passed from the last activity.
*/
private void GetUserInfo () {
Intent Intent = This.getintent ();
Bundle Bundle = Intent.getextras ();
Long userId = Bundle.getlong ("user_id", -1l);
String userName = bundle.getstring ("user_name");
}

}

We can see that this is a cumbersome way to pass data, especially when there are many active and frequent shifts. The use of application objects is more concise and convenient when the entire application of public data (such as login information) is the same and needs to be used between different activity.

Related Article

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.