Saving global variables in Android programs

Source: Internet
Author: User

Sometimes our app needs to save some variable values for the program to call anywhere, so we'll use the method of saving the global variables. Here are two ways for everyone to refer to.

1: Use Application to save global variables


1.1 Define a class Appcontext inherit from application

Package Com.example.demo_001.app;  Import Java.util.hashmap;import java.util.map;import Com.example.demo_001.user;import android.app.Application;/**   * Creation Time: 2015-8-25 pm 3:09:27   * Project name: demo_001   * @author Xu Hiayun  * @version 1.0    * @since JDK 1.6.0_21   * File name: Appcontext.java   * Class Description:   */public class AppContext extends application {private string B;//save String Private User User Save user entity Object Private Map Innermap = new HashMap (); Save data for map type public void OnCreate () {//Initialize string b = "arthinking";} Public String Getb () {return B;} public void Setb (String b) {this.b = b;} Public User GetUser () {return user;} public void SetUser (user user) {this.user = user;} Public Map Getinnermap () {return innermap;} public void Setinnermap (Map innermap) {this.innermap = Innermap;}}  <span style= "color: #cc0000;" ></span>


1.2 declaration in Manifest.xml application

   <application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "       <strong><span style=" color: #cc0000; " > android:name= "Com.example.demo_001.app. AppContext "</span></strong>        android:theme=" @style/apptheme ">        <activity            android: Name= "com.example.demo_001.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>

1.3 Assigning and taking values to global variables in a program

<span style= "color: #cc0000;" ></span> @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//assignment final AppContext app = (AppContext) getapplication ();//Assigning a value to a string App.setb (" Hello,world ");//Assign the user object user user = new user (); user.setage (+); User.setname (" arthinking "); App.setuser (user);// Assigns a value App.addparam ("Company", "Alibaba") to the Map object;//value string str = APP.GETB (); User user2 = App.getuser (); String company = (string) app.getparam ("Company");} <span style= "color: #cc0000;" ></span>


2. Saving global variables using normal classes


2.1 Defining a Class

public class Data{private static string a = "Feiyangxiaomi";p ublic static string Geta () {return A;} public static void SetA (String a) {data.a = A;}}

2.2 Values and assignments

Call the appropriate set and get methods.


Both methods of testing were passed, and the individual suggested the first way.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Saving global variables in Android programs

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.