Storing logged-in user information with the Application object

Source: Internet
Author: User

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

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.