I want to know if is there a way to obtain a reference of an activity, from an other class, without passing it as parameter.
In particular, to open an Asset I need that the activity invoke getAssets(), but I haven't any reference to the activity.
The appropriate way is to pass the reference. You can store it as a static variable somewhere, but this is a very unrecommended method. If you don't have a reference, consider changing your design.
I'm not saying that only as an educative / theoretic argument only, but from (bad bad bad) experience.
Related
Sorry, but showing through images is much better, okay?
The explanation follows below:
But if you declare the entire variable as a global variable, the following error occurs:
Please, what to do so that the variable value inside the blue rectangle goes to the value inside the red rectangle variable, in the first image, in the Android?
You don't. A variable declared in a method is only visible in that method. You either need to make it a class level variable, or you need to find another way of doing things.
The reason you're seeing the NullPointerException is that you haven't called the function yet when you try to use the class level variable. That means the variable isn't set, so its the default value of null. And obviously you can't dereference a null variable.
Your problem here is you have some circular logic. You're trying to set a change listener to a firebase reference, but trying to get the id of the reference inside the change listener. That's not going to work- how do you get a reference when you don't know what you're trying to get a reference to? Either you missed some other way to get that id, or you need to rethink what you're doing.
A Path object comes with many methods that changes it (moveTo(), lineTo(), cubicTo(), arcTo(), reset(), etc.).
Is there a way to freeze the object once you have it the way you want it, before you pass it onward?
Something along the line of CGPath in objective-c?
No. Immutable means that once the constructor for an object has completed execution that instance can't be altered.
Still, you can subclass Path and add pseudo-immutability yourself if that's really needed but there is no built-in mechanism out of the box.
Not ideal, but an alternative solution to the problem you are describing is to copy of the original Path using the constructor:
Path copy = new Path(originalPath);
Then, you can pass the copy the other thread without worrying if they change it while you are reading the original Path.
I was wondering what the difference between database.getReference("foo/bar/123") and database.getReference("foo").child("bar").child("123") is?
I'm assuming that the later one will load the complete "foo" object whereas database.getReference("foo/bar/123") just loads the "123" object?
Is my assumption correct or what is the correct / most efficient way to only load data of "123"?
The two are equivalent. You can inspect this manually this by printing the toString() format for both References.
References are cheap - there's nothing inefficient about either solution. Neither one has yet loaded any data. A Reference is just a pointer to a location in the database.
It should not make a difference, a reference is not actually accessed when instantiated. This is the most relevant document I can find,
https://firebase.google.com/docs/reference/node/firebase.database.Reference
The docs don't say it explicitly, but requests are only performed when using the .set() or .on() methods
I'm making (my first) android application and I'm a little bit puzzled with the use of db4o.
I have one activity in which I have a listView, and let the user select an object. Then I pass this object trough a series of intents to other activities (to populate its fields) and then back to the main activity.
But the problem is, when I pass an object (it is serializable), the object I get out of the intent is not the same as the one I put in. (different id, when I check with debug).
All the fields are the same, but it's just not 'the same' object.
So when I try to store the updated object in the db4o, it doesn't recognize it, and stores a double.
I've figured out two workarounds:
Also pass an 'original/unmodified' object, and use it to get the db4o reference (through QBE), and then updating the fields of that object with the values of the changed object.
Using global variables so I don't have to use intents (to pass the object)
But both seem really bad to me? What could be a real solution, instead of a workaround?
You could try using a singleton to store your object and the fields that other classes (?) need to set so everyone has access. I'm not clear of your use of intents in this explanation.
I need to get the context to be able to get a resource. Like this:
getApplicationContext().getResources().openRawResource( R.raw.texture );
I've seen the getApplicationContext() in the android documentation but when I try to use it in the above code it doesn't work - it doesn't exist.
I can send the context through functions to get it to where it's needed and it works. However, I find it cumbersome to send a variable through many functions that doesn't need or use it. Then I would rather just try to get it in the function that do. But the getApplicationContext(), as in the android documentation, doesn't work - http://developer.android.com/reference/android/content/Context.html
So how do I get the context so I can read resources? Or are my only option to send it through all my functions?
getApplicationContext() is a method of a Context. You have to have a context to get the resources. That's just how it works.
Just makes sure you're not storing a reference to your context anywhere or you could cause a memory leak.
In your activity you can use getResources() method immediately. For example
getResources().getDrawable(R.drawable.logo);
If you want to get some resource in some other class not in activity, you should pass context link in other class from your activity. For example
Util.convertLogo(this)
or
Util.convertLogo(getApplicationContext())