1

I have multiple classes:

  • the main activity
  • service
  • settings

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?

  • should I start settings with an intent and make it return the value of delay?
  • should I make a getter which can return the value after creating an instance of settings with something like mysettings.getdelay()?
  • or is there another (/better) way of doing 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.

biepbiep
  • 207
  • 2
  • 11

2 Answers2

1

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">
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • SharedPreferences will store my data in a file so that my settings will be the values I gave them the previous time I opened my application. Right? I could use that but what would I do if I don't want my app to store the settings? – biepbiep Apr 22 '13 at 18:29
  • Data is stored in a private area and can only be accessed from your app. If you need only temporary "settings" during the runtime of your app, you can use your own Application class and use variables there to keep data for the life-time of your application. – Ridcully Apr 22 '13 at 18:33
  • I'm sorry but I don't understand what you mean by "your own Application class" – biepbiep Apr 22 '13 at 18:38
  • I added an example to my answer. – Ridcully Apr 22 '13 at 18:44
  • ah thanks, now I understand :) but you declared it static and as far as I understand, you shouldn't change the value of static vars, correct? Or am I missing something here? and you think that could be the best solution? – biepbiep Apr 22 '13 at 18:49
  • you can change the value of static. use them all the time in java. it just means there will be only 1 variable of that name in that class. – tgkprog Apr 22 '13 at 18:58
  • is there be any reason why I should create an application class instead of just putting the variable declaration in my "main" class/activity? – biepbiep Apr 22 '13 at 19:36
  • declaration in my "main" class/activity is your application class. its anyway a runtime variable keep it any where it makes sense and add a one line comment – tgkprog Apr 22 '13 at 19:57
1

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.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • settings is indeed an activity. it is the code behind a sort of dialog (which I defined in an XML) which allows you to change the settings. So, I'll use your first suggestion then. Thanks – biepbiep Apr 22 '13 at 18:44
  • You're welcome. About your `Settings` dialog. You may know this but you can make an `Actviity` appear as a `dialog` by using `android:theme="@android:style/Theme.Dialog"` in the `activity` tag of your `manifest` – codeMagic Apr 22 '13 at 18:48
  • I already did that but actually I don't really know what it does. I think it just makes the layout appear in the center of your screen with a minimal width and height. Or does it do anything else? – biepbiep Apr 22 '13 at 18:58
  • Yeah, it just gives it the "appearance" of a `dialog`. This can help the user feel like they haven't left where they are at in the app – codeMagic Apr 22 '13 at 19:00