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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
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
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?
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
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">

8 Comments

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?
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.
I'm sorry but I don't understand what you mean by "your own Application class"
I added an example to my answer.
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.