declaring application path as a static constant - android

I'm trying to declare all constants used by my application in a separate constants class. I'd like to declare the application path but can't figure out how to do it in a static way.
How do I replace the "this" to something that will let me get the path?
public static final String DL_PATH = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/";

How do I replace the "this" to something that will let me get the path?
You don't. There is nothing that you can use for this.
You are welcome to assign the value to DL_PATH from onCreate() of a custom Application. Or, delete DL_PATH entirely and just call getFilesDir() as needed from your various components (e.g., Activity, Service).

Related

why database name must be static?

My question is very simple ! I have seen several applications that define database name(or table name) as a static variable !
why ?
like :
private static final String DATABASE_NAME = "database" ;
can I define it as a final variable ?
Possibly this way you want to say that your DATABASE_NAME is not part of every instance of your class, but a part of the class itself. That would mean that each time you declare an instance of your class, each of those classes won't have a copy of that attribute but they will share that attribute instead.
Quite logic, this is meant to be a static field so why make an instance of it? And yes, it should be a final variable, as it is unlike it will change at execution time.
For convenience. The DB name never changes while the app is running or between instances of the same class - it can only change between app versions. This way if you change the name of your database you won't have to search for it in the entire app - just modify the DATABASE_NAME variable.

Why STATIC attribute for some parameters in java class?

I know what a static variable in java and I know when to use it. But I see often appear in the android source code, the parameter private static final String TAG = "MyActivity" Do we really need this variable is kept in memory? I want to know why we use it in this way?

Handle soft labels in Android

I am building an application and use labels in the following areas.
1. Component Names (TextView, Buttons etc)
2. Buttons in alert boxes
3. Parameters to AsyncTask
4. Key names in Intent extras
5. Log tags
I have got strings.xml files to store and use labels. I am using using it for category 1. What do you think is the best option for others?
res/strings.xml or
Constants class ?
I'll suggest to you what I normally do:
For everything that the user will read on the screen or be referenced in anyway from a XML layout, use strings.xml. I reckon that includes your 1 and 2. But remember that for AlertDialogues you can also use android.R.string.ok or android.R.string.cancel, etc; that will vary from use to use.
Also for project config values, or API keys, you could have a config.xml with String, Integers, etc. Something that makes easy to later on change those values without diving into the code.
For AsyncTask, it varies a lot of what work it is doing, but usually a network, DB or disk operation, and I try to include private static final String URL_PT1 = "http://..."; etc, in the classes themselves, as they are the only interested in knowing that address (or DB columns, or file prefix)
For key names (both in Intent and Fragment arguments) I use private static final String KEY_VALUE = "renjith.Key.MyClassName.VALUE"; and then create a public static Bundle getIntentExtras(String value) inside that class that will properly allocate the value in the extra. That's because only that one class needs to know about it's KEY values, there's no reason to make those public accessible, that will just create errors for outside classes doing it wrong.
For Logs (5), create on every class a private static final String TAG = "Renjith.ThisClassName"; (and for Fragments make the TAG public to be used in transactions). That way you can create a filter in LogCat Renjith.* and you can easily identify from where this log is coming from
edit:
For sharedpreferences it depends:
for GlobalSharedPreferences I would create one class to handle them with static set/get methods that must receive Context and all keys private static inside that class.
For local shared preferences (that only one class is interested in saving/retrieving) I would handle in a SharedPreferences file with the same name as that class and all the keys as private static inside that one class.
Again, same philosophy, only the classes that really need to know about those details will know about them, and abstracting to the rest of the classes with direct method calls.

Get String from value folder to class android

I want to use a particular string found in the Values folder into normal classes. This is for my localization process with different languages.
How would I do this?
Use the getString() method on a Context (such as an Activity), specifying the key in this form: R.string.you_key
If you need to get a String outside of an Activity, like in a helper class or something, just make sure you pass in a reference to a context.
Access the value by calling getString(R.string.your_string);
Ex.
String myValue = getString(R.string.your_string);
obviously replace "your_string" with the actual name you gave the string

Settings variable in Android

Is there a way to have an global settings variable for an android application, which is accessable as well from any help java classes without giving them context?!
I try to explain what I mean.
I have an application version as string value in strings.xml
I can get its value from every android activity, but not from help java classes withought giving context
What I do now, is saving it in a static variable of my first activity, but it seems, that sometimes it will be erased and set to null.
May be I do something wrong?!
Sorry for newbie question.
And thank you in advance,
Mur
P.s.
I wrote a small tutorial for this topic, to show the solution.
A variable declared as public, static, and final will be visible to all of your classes and never get erased.
public static final String VERSION = "1.2.3.4";
You could make a public static variable in your application class that you fill with the value from strings.xml in the onCreate method. The application class is a singleton and will be the last thing that is killed as part of your app so it will always be there and if you make it public static there will be only one instance.
I'm guessing that you have a JAVA class for some common utility functions. You get the value of your string using a context in your Activity/Service and then pass in that value to the JAVA class function as a parameter.

Categories

Resources