The Application object is ideal for storing data related to the entire application, such as app version, app login account, data cache, etc.
If the activity of the switch is very frequent, almost can and a Web site between the different pages of the same, then the different activity between the need to store public information (such as only one currently logged on users) and data transfer and so on.
Using the Application object to store logged-in user information, you can easily get logged-in user information between different activity.
First, create a new Java class to inherit the application class: Baseapplication.java
Import android.app.Application;
public class Baseapplication extends application {
Private String Login_user_name = "";
Public String Getloginusername () {
return login_user_name;
}
public void Setloginusername (String login_user_name) {
This.login_user_name = Login_user_name;
}
}
Then, specify the app's Application object in Androidmanifest.xml
......
<application
Android:name= ". Baseapplication "
......
Finally, using the Application object in the activity, use the activity's Getapplication () method.
Import Android.os.Bundle;
Import android.app.Activity;
public class Mainactivity extends Activity {
Private Baseapplication mapplication;
@Override
protected void OnCreate (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 same
Mapplication = (baseapplication) getapplication ();
}
/**
* Usually only in the login interface to set the login user information, in other activity
* Login user information can be obtained as long as the Application object is used
*/
......
Save logged-in user information to the Application object
Mapplication.setloginusername ("login name");
Get Login Name
Mapplication.getloginusername ();
}
Storing logged-in user information with the Application object