sravani
sravani

Reputation: 11

Android :Declared Global variables becoming null while running the app?

I'm using this class.

I'm new to android, I have declared global variables in one class. and I'm using that variables across the multiple activities,but these global variables are becoming null while running may app only in low memory devices and version I'm using for testing 2.3.6

How can I solve this problem?

Upvotes: 0

Views: 1984

Answers (3)

Mayur Raval
Mayur Raval

Reputation: 3275

I think you are creating instance of that class each time in your all activities. declare all variable as "public static final" in that class

if you want to use that global variable than you just code like this in your activities.

 SQRUtils.downloader = // change which you want//

SQRUtils.mGson = //if you want to change mgson value//

and you can get all data each time in all class .after stop that application this variable will be delete.

Upvotes: 0

Amritesh
Amritesh

Reputation: 96

There are few solutions to it:

  • Extend the Application class for storing global values, and that class will be available across you activities.
  • Save in SharedPreferences(This is the easiest and reliable way to save and retrieve global data).
  • Save in database(Sqlite db).
  • Also, you can save in SDcard or internal memory.

    Let us know How you are trying to store value and at which point it's value is getting reset.

Example code for First option:

public class GlobalValue extends Application {

     private int value = 0;

    public int getValue() {
        return value;
    }

     public void setValue(int value) {
         this.value = gameScore;
     }
     public void incrementValue(){
         value++;
     }

 } 

Declare this class in Manifest file :

 1: <?xml version="1.0" encoding="utf-8"?>
    2:    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3:     package="com.example"
    4:     android:versionCode="1"
    5:     android:versionName="1.0" >
    6:
    7:      <uses-sdk android:minSdkVersion="14" />
    8:
    9:      <application
    10:         android:icon="@drawable/ic_launcher"
    11:         android:label="@string/app_name"
    12:         android:name="GlobalState" >
    13:
    14:         <!-- component definitions -->
    15:     </application>
    16:
    17:   </manifest>

Upvotes: 0

fida1989
fida1989

Reputation: 3249

Try Like This:

Create a shared data class:

SharedData.java

public class SharedData {

    private static SharedData instance = new SharedData();

    // Getter-Setters
    public static SharedData getInstance() {
        return instance;
    }

    public static void setInstance(SharedData instance) {
        SharedData.instance = instance;
    }

    private int notification_index;


    private SharedData() {

    }


    public int getNotification_index() {
        return notification_index;
    }


    public void setNotification_index(int notification_index) {
        this.notification_index = notification_index;
    }



}

Declared/Initiaze an instance of class globally in classes where you want to set/get data:

SharedData sharedData = SharedData.getInstance();

Set data:

sharedData.setNotification_index(123);

Get data:

int n = sharedData.getNotification_index();

Upvotes: 1

Related Questions