Recently I ran into a problem when trying to load an bitmap outside of my main class. I found that within my main class I could pass this as a Context to any given function allowing me to call getResources() from within that function. There are a couple of things about this which don't make sense to me.
The keyword "this" simply refers to the current object the function is running in, right? If so, how can I pass my main class as a Context by using "this"? It doesn't even have a Context in it. I am using "extends Activity", but Activity doesn't seem to contain any function called getResources() in it either.
I found a workaround which allows me to do what I want a bit easier which is to declare a public static Context appContext; within my main class. Then, within onCreate() I set appContext = this; Then, from elsewhere I can call MainActivity.appContext.getResources() whenever I need it. So, I really have a few questions here.
Why are MainActivity(my main class) and appContext not essentially the same thing when appContext is set to "this" from inside MainActivity (There is no such thing as MainActivity.getResources())
Is this unsafe to do? Could this cause any potential problems in my program?
Is there a way to load images without having to use getResources()?
What is the proper way to show my code in this website? The standard I am used to ([code][/code] tags) don't seem to work properly in the preview, so I am assuming it is handled differently here. The formatting help page says to simply use four spaces, but that doesn't seem to show any difference in the preview section either.
EDIT:
I just read in another thread somebody said
now everything depends on your main activity's onCreate method having been called.
That got me thinking. Under what circumstances would onCreate not be called? It seems like if it wasn't called the program would stop working properly in many ways not related to having a static variable initialized inside it(ie. setContentView would not be called).
So assuming that there is nothing wrong with doing it this way, the only drawback I can find, is that "MainActivity.appContext.getResources()" is a lot to type to call a function. Is there any way to create a sort of alias for this? I suppose I could create a function which simply calls that, but that just seems silly.
If you look here you see that activity is a subclass of Context.
You can format code with a backtic, or just use the {} buttons on the editor. See the markdown manual for more info.
Related
I'm integrating stripe in android java. Can anyone help me sample stripe code in java.
As I'm new to kotlin.
Using default stripe classfor add card "PaymentMethodsActivityStarter"
I'm unable to get response in onActivityResult
You must look at the code carefully, analyze it and then proceed. If you concentrate, it will become simple for you in no time. Let's go through an example:
class MyApp : Application() {
We know that this is a class, and its name is MyApp. A quick Google search would tell you that the : here just means that the class is extending whatever's on the right of it. Therefore the corresponding Java code should be
public class MyApp extends Application {
Now we go to the next line
override fun onCreate() {
States that an override tag is involved - a method is being overridden. fun in Kotlin means the following should be a function name. Once again, a Google search away. We see that it has no parameters, and no returns. Now you can make the Java code
#Override
public void onCreate() {
Now we move on
super.onCreate()
Which is calling the superclass method, same in Java.
PaymentConfiguration.init(applicationContext,"pk_test_TYooMQauvdEDq54NiTphI7jx")
I'm not familiar with Kotlin, but I can see that some form of init is being called. Parameters are a Context of some sort and a String. A Google search told me that init is basically calling the consturctor of a class. Therefore I can immediately try out
new PaymentConfiguration(getApplicationContext(), "pk_test_TYooMQauvdEDq54NiTphI7jx");
If that doesn't work, then Google more to understand what the code means instead of searching for a specific Java equivalent. If you still have problems, then post on here again with examples and some code and include the solutions you have tried and offer thoughts on what you think the issue could be. This is how we become better programmers that can adapt to and overcome problems.
I'm a beginner Android developer. I'm still learning new things every day, and while learning I have heard this question quite frequently: What will happen if we send View object as parameter within OnCreate Method? I have searched about it but didn't find anything helpful. I just want to know is it really possible and if so then please explain the scenario.
oncreate() called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state.
I save to SharedPreferences within a single Activity several times. Each time I do reads or writes, I pass the Context. Following are the two options I am thinking about - which one is better (or is there a better way)
Everytime I need to pass context to a function in another class (for ex a class writing to my SharedPreferences), I am passing the this getActivity().getApplicationContext().
Should I just save a variable once in the onCreate() of the Activity (i.e. Context ctxt = getActivity().getApplicationContext()) and then use the variable ctxt throughout the Activity whenever I need to pass context.
Which way is technically better? Key is - should I be accessing getApplicationContext() each time. I am guessing that is slow (& unnecessary) and also probably causes crashes (I've been seeing some logs from my users that I am unable to reproduce but the stack traces always point to lines containing getApplicationContext())
The Activity itself can be used as the Context. Simply pass this. You can pass ActivityName.this inside an inner class.
getApplicationContext() returns "the context of the single, global Application object of the current process."
check this post its what you want, and you just use the Activity as Context and there is no problem calling getApplicationContext() anytime you want, you just might need to find alternatives to that and maybe use getBaseContext() or the activity itself
One can call and see private methods using reflection by calling getDeclaredMethod.
What can I do if I don't want my private method even to be displayed outside my class?
The only option is to inline it so it doesn't appear. Methods are always visible via reflection.
You're out of luck, unfortunately. You could obfuscate the name (simply name it to something non-obvious) but other than that you can't really do anything.
Note also someone can decompile your class. So any functionality is visible, and in this case obfuscation will hide the intent, but not the code.
I am currently working on a single activity Android app that uses a lot of shared UI objects (like Button, etc). At the moment, I have these UI objects declared as private non-static class variables. The alternative to this would be to continually pass the objects from method to method and modify them that way. It is currently unclear to me which option should be preferred, as it seems to me that using the maximum encapsulation form would cause me to do quite a bit of argument passing. Can anyone shed some light on this?
Generally I like to think of encapsulation as hiding data within a single class. If multiple methods are accessing that object within the class, that doesn't really violate encapsulation principles. Once you start leaking it outside the class, that's when encapsulation problems occur.
With that said, it is perfectly fine to have a private member which is a Button and multiple methods can access that button.
As above Jeff said You should go for passing arguments as you are inside one activity as you have mentioned in your question and encapsulation is always the first thing to be recommended.I will suggest to do one more thing if you can :
Define one function which will accept the color code or color type, and call this function from all the functions where you want to change the button text color.In this way code can be in one place which is performing the same operation i.e. changing the color.
It depends if those private non-static variables that you want to pass as arguments actually make sense to become the properties of the class.If you think it makes sense and have design control over their updation/modification you can go ahead and declare them as class members