Android officially recommends transferring data to Global Objects

Source: Internet
Author: User

Although static variables can be used to transmit any type of data, it is not officially recommended. If a large number of static variables (especially those that occupy resources, such as bitmap objects) are used in the class, memory overflow may occur, it is also possible that the code is difficult to maintain and confuse due to the appearance of static variables in many classes. Therefore, global objects can be used for transmission. This method can completely replace static variables.

Global object scope in Android: unless the android program is completely cleared out of memory, the global object will be accessible all the time.

1. First create a global class:

package cn.jiabin.demo.view;import android.app.Application;public class MyApplication extends Application {public String name;public int age;}

2. Configure the global class to the androidmanifest. xml file:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.jiabin.demo.view"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:name=".MyApplication" >        <activity            android:label="@string/app_name"            android:name=".MyDemoActivity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".A" />    </application></manifest>

3. Use it in a normal class:

Myapplication MyApp = (myapplication) getapplication (); MyApp. Name = "James"; MyApp. Age = 23;

It is recommended that data be transmitted in this way.

All rights reserved. If you need to reprint it, indicate the source. Thank you for your cooperation. Shenyang -- binzi.

Note: QQ technology exchange group: add one if you are interested in 108614806.

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.