I have a customview with a field(it's name is step) that set in constructor(really I pass it's values as parameter when I create an instance).Also I create some Bitmaps in constructor width of Bitmaps is correspond to that field(that comes from constructor).I know how to create Bitmaps,but I have to crate many instance of that class,so each instance has it's Bitmaps and so I will have many Bitmaps in run time that usually are same(if step field be same between classes).I thought that I can create a static class that store bitmaps,but as I said,it is possible that they be different.
Is there any way to decrease the number of Bitmaps that are created?
Sounds like a job for a Map, HashMap (or maybe WeakHashMap) in particular. The values would be the bitmaps and the keys would be the step values. Placing it in a static instance of a class is reasonable.
Related
I have a NFC tag and I have to read from it a text, and save this text some where to use the parameters in it in many activities and fragments.
this parameters I should be able to delete or overwrite it when a user decide to exit the program or to read another tag.
I didn't like the idea to transfer this parameters over the activities since they are constant in the whole session for example the ID number of the tag and the manufacturer of it.
I thought also to creat a file in Assets and read it every time, but I thought there should be better way to solve this problem.
There are several ways to how you make the values accessible throughout the project.
Using Shared Preferences:
You can use shared preferences, where you can create variables for your fixed values such as TAG_ID and MANUFACTURER_ID. Every time you tap a new card you can update them, or anytime you expect them to be changed.
Using a Model Class:
You can also create a Model Class (May be Singleton as well) which holds the TAG_ID and MANUFACTURER_ID. Initialize the object of this model class every time there is new Tag tapped. You can also access/change them anytime using getters and setters.
Using Static variables:
You can define static variables to store your desired values and you can simply access them wherever and whenever you want to. This will workout only with less numbers of variables. Increased static variables could affect the performance.
I'm pretty new to programming for Android so if this is the wrong way to go about it anyway, feel free to comment about my bad practice.
In my App, I have different Activities that use the same values. Also, I have some classes that have static methods, used in multiple activities. For example to generate ImageViews from a "URL".
One of the values I would use in almost all of my app is this snippet: (taken from another question on SO)
getApplicationContext().getResources().getDisplayMetrics().density
This particular value for example is needed for every activity. But it's also needed for the same task. So I use a static class that has the appropriate method for that task. However, that static class does not have a "getApplicatioContext", obviously. So I would need to store the value somewhere else.
My question is, is there a designated or at least optimal or clean way to store such values and initialize them on App startup?
As I understand it, I could use the "onCreate"-Method of my launch activity and initialize some static class or something like that. However, that seems a little dirty to me. Maybe there is some "earlier" time than the launch activity's onCreate() and a better place to store the values than a custom static (or singleton) class?
It is hard to tell you exactly what the best practice is when we don't know exactly what you're trying to accomplish overall, but I have a point to make that I think is abstract enough to be relevant here.
Always use the current Context. Don't store a Context or otherwise try to "get around" having to pass a Context argument to methods.
(Of course there are always exceptions, but this is a good rule of thumb.)
In this particular case, I'd recommend creating a class with a static helper method:
public class MyUtils {
private MyUtils() {
throw new UnsupportedOperationException("static methods only");
}
public static float getDisplayDensity(Context context) {
return context.getResources().getDisplayMetrics().density;
}
}
And you could call it from any Activity like this:
float density = MyUtils.getDisplayDensity(this);
Or from any Fragment or View like this:
float density = MyUtils.getDisplayDensity(getContext());
Often, when people post questions about DisplayMetrics and density, what they're really trying to do is convert dp units to px units. For example, they know they want something to be 48dp, but they need to pass a pixel value to some Android method, so they want to multiply by the display density.
In these cases, you're better off using dimen resources. You can define:
<dimen name="my_dimension">48dp</dimen>
And then you can get that in px units easily by calling
getResources().getDimensionPixelSize(R.dimen.my_dimension);
I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.
I want to create a utility class that loads images contained in the assets/ file structure. It would be tiresome to require all calls to it, or any other utility class calling it, to pass along the Context all the way from the UI.
Would it be possible to just pass it down once, and store it (as long as the utility class is static or a singleton)? Or would that have devastating consequences memory wise?
Also, so no one asks, I'm using the assets/ folder instead of res/ because I want sub-folders, and to access images by name dynamically, which I can't do using resources to my knowledge. For example, I want my sprite object to load all images in the specific sprite's folder, rather than hard coding R.drawable.sprite1front, R.drawable.sprite1leftside, etc.
*edit - Copying the AssetManager would also be fine, if that can be done.
You can use a singleton or a static class for your utility class, but you should initialize it with a reference to an Application object, not an Activity. (Keeping a reference to an Activity is a good way to leak memory.) The best way might be to subclass Application and use that as your utility class. You'll have to declare your Application class in the manifest.
When adding an object to a Bundle in order to save it in onSaveInstance does it save the state of all the static variables or will then be reset when I reload using onRestoreInstance?
As an extension of the question, is it possible to save a non-instantiated class. In other words just the statics?
I am using it for a card game and since I will only ever have one deck of cards it seems silly to have to instantiate it when I can use all statics.
From an OO perspective, it would make more sense to model your deck of cards on the Singleton pattern rather than making it static. Then you can save that object in the Bundle and restore it without having to worry about the static issue. If you care about such things, this method also makes for 'better' object oriented programming.