biepbiep
biepbiep

Reputation: 217

Proper way of accessing variable Java/Android

I have multiple classes:

With settings it is possible to change a certain delay (variable) which I need (I need the value of that variable) in service.

How should I implement this?

thanks

[EDIT]
I ended up using something like Ridcully's answer. Only difference is that I didn't give a public declaration and used a getter and setter instead. People always told me to use getters and setters.

Upvotes: 1

Views: 209

Answers (2)

Ridcully
Ridcully

Reputation: 23655

For your settings you should use Android's standard for this purpose - SharedPreferences. Read this API Guide for a start.

EDIT:

For storing data only for the life-time of your app you can also use your own Application class and use variables there:

Custom Application class:

public class MyApplication extends Application {

    public static int delay = 0;

}

In the AndroidManifest.xml:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".MyApplication">

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

should I start settings with an intent and make it return the value of delay?

If Settings is an Activity then yes. Ccheck into using startActivityForResult, assuming your Settings class is an Activity. You can start that Activity and return an Intent with data and change your variables based on what is returned.

should I make a getter which can return the value after creating an instance of settings with something like mysettings.getdelay()?

If it is a regular class and not an Activity then you probably just want to set up a getter() method and return a value based on certain params passed.

This is a couple ways you can get the variables. If you then want to store them permanently then Ridcully's answer is appropriate. For more help, please post relevant code and a more specific question. Hope this helps.

Upvotes: 1

Related Questions