I have an application, which is including a library which exposes set of methods via a public class. Internally, this library uses the context passed on to it by the application.
Question: Since the public class in the library does not extend Activity, the methods use the Context object passed on to it by the application. In this case how to communicate the result/data back to the application? The context object does not have startActivityWithResult() method.
Any idea?
There is no any method like startActivityWithResult() for Activity, the method is startActivityForResult().
If your context is Activity reference then you can use startActivityForResult() method in that class. just use like,
((Activity)context).startActivityForResult();
Related
Hey I'm using Android Studio with Kotlin and I have an activity and a service that both needs to call same methods, so I created an object with theses methods in it, which would be like a utils class.
The problem is that the methods depend on a field that needs the application context to be created, and the only way I found to retrieve the context was to pass it through the constructor, but as it's an object I can't do that.
How could I keep that object behaviour and retrieve the application context ?
In Kotlin, an object behaves like a static util class(not exactly at bytecode level) so there are only a few options that you could do
Pass the the context in every util method in the object which needs the context
Make an Application class and keep a global reference of the application context and use it everywhere by making it lateinit var and initialising it in Application`s onCreate method
Or use DI for injection of Application context and instead of using Object use Singleton scoped utility that is injected everywhere
How I can get application context outside activity and without extending application class.
class A{
public static B b = new B(App context here);
}
Objetc b must be as a field
No you can't get context without extending Application or Activity by the given example. All that you can do is to have a static method in a class (extends Application) that would return context and then pass that method as a parameter to your B().
BTW, I did not get your intention of doing this. Can you detail out what exactly you wants to do.
Thank you
It is not possible.
If it were, it would mean that any Java class would be able to access an android.content.Context instance, even when is not related to anything in Android, for example, a Java EE application.
What you can do, is, have a static reference to a Context (an Application or an Activity) in a centralized place inside your app, but I wouldn't recommend it because it would cause multiple memory leaks.
I have a Library Project and a project each for the free and paid versions for one of my apps. In the Library project I have a 'Base Activity'. This 'Base Activity' must start child activities depending on whether it's the free or paid version.
What I did was just before I fire the intent with startActivity() I call a method in 'Base Activity' which must return the intent. I override this method in my 'Base Activity' subclasses (the paid and free versions) and create the intents like this:
return new Intent(subClassOfBaseActivity.this, ChildClassA.class);
and:
return new Intent(subClassOfBaseActivity.this, ChildClassB.class);
Now, my question is, it is ok to create the intent by passing it subClassofBaseActivity.this instead of BaseActivity.this ?
Is this method ok overall?
When you create a new Intent you need to pass the Context and the Activity.
Now the context is an interface which allows the application to access some resources.
Basically in your case , you're passing a context and activity as it supposed to, but you ask if you need to pass the baseActivity as the context.
So basically I think that because of this line in android developer:
Interface to global information about an application environment you need to pass the base activity as the context..
on the other side, the subClasses are probably inherit from the baseClass so the context should be the same, but it will be more readable and clear when you passing the baseClass as the context.
for more information about Context
http://developer.android.com/reference/android/content/Context.html
Yes, it is ok. This constructor take a first parameter Context which Activity extends. So basically you are passing argument as Context not as an Activity.
Thats fine.
Java will cast the first argument into Context (Activity extends Context) so it doesnt matter. You can also put getBaseContext() insteat of Activity.this there..
In some places we were giving like "DatabaseUtil db=new DatabaseUtil(DailyPlanView.this);" where DatabaseUtil is the class with the constructor argument is context. But if we create the object for the DatabaseUtil class in the DailyPlanVIew class we are using the above code. My doubt is what is the use of the context and instead of passing the context object as argument why we are passing "this".
Whenever you are dealing with Context, its important to understand its used in everything. From using a database to obtaining system services. This is required by the way android works with Context. Specifically when you are passing this you are basically passing the class that encapsulates this statement.
class MyActivity extends Activity
{
onCreate(Bundle bundle)
{
View v = new View(this);
}
}
passing this refers to the object that encapsulates it. This is a Object oriented concept... Where this is reffering to MyActivity. One thing to keep in mind when passing context is ensure that you are passing the correct kind. Some Context objects have a longer lifespan than others and if not managed properly can lead to Context leaking. Specifically in this example, this works because Activity extends Context.
The differences occur in the View class.
getApplicationContext()
getBaseContext()
this, which in the Context of an activity has the life span of an Activity (Example above)
One thing to add about Context is that it is basically a reference to the current Application and it's specific data.
Some more information about context can be found in this thread:
What is 'Context' on Android?
I have a helper class that I need context so I can access the SharedPrefences. Other posts recommend passing in the application context on instantiation of the helper class. So I made that change, it works very well except within a tab activity. The tab activity need to call a webservice to determine what data to display. The helper class makes the webservice call.
You can call getContext() from any activity. If the helper class is defined as a subclass of the activity, it can call it directly. Otherwise, passing the context through instantiation would be my second choice. I agree, it's not pretty passing contexts everywhere. There are probably some complicated OOP patterns you could use to avoid this, but I can't see it being an advantage overall.
If you get a null pointer you might be calling the function too early. In what function are you calling it?