how to get fragment object from context - android

Is it possible to get the fragment object from the context object? I am basically trying to access the fragment object from a SearchView class that i extended. Since context is the only object passed to the SearchView constructor, i was hoping to somehow get a reference to the fragment from it.
I know that we can get it from within an activity by using getSupportFragmentManager().findFragmentById(R.id.xxx). But getSupportFragmentManager() is available only in a FragmentActivity class. Is there someway to access the fragment from another class?

If you don't mess with the normal way a View is built then the Context reference that you receive in the constructor of a View is the Activity(a Context) where the View is being used. So, you have the option of casting that Context to an Activity reference and from there you can use one of the various ways to access the desired Fragment.

Related

How a Fragment can be associated with a Context?

In the documentation of the android.support.v4.app.Fragment class (not the framework's class) for the getActivity() method stated that the returned value may be null if the Fragment is associated with a Context.
How an Fragment can be associated with a Context? Is not the FragmentManager the only way to attach an Fragment to something? But the FragmentManager can be obtained only from Activity. Or not?
How an Fragment can be associated with a Context?
You must have heard of FragmentHostCallback. If you haven't check out the link.
In a simple way, it is an integration point with a Fragment Host. When I say Fragment Host, It is an object that can hold Fragments. For example an Activity. In order to host a fragment - one must implement FragmentHostCallback.
However, I haven't come up with any ideas about how Fragment can be implemented in non-activity objects. Will see in future may be...
So that way, getActivity() will return null on non-activity objects.
PS,
Always go for getContext() if you are requiring context rather than activity
As I understand, you need the context inside of a fragment.. If so have you checked the method getContext() method inside of a fragment?
Also getActivity() can be null if you are referencing it when the fragment is not attached to an activity. Have a check of the fragment lifecycle to learn more.
Hope I helped

The right way to pass an object to fragments?

I want to pass an object to fragments
when the object shouldn't or can't recreate in a fragment (e.g. LruCache).
Is it possible without using static variable?
since passing variable may not always same object
Edit:
To make it clear I don't want the object to be null when activity recreated
For passing object to fragment you should use interface have a look at this link.
You can pass object via constructor and getter and setter also but in case of app resume object might be null that will cause NullPointerException on app resume.
Edited
If you are working with LruCache and want to share its data within fragments. I will suggest you to create a fragment without a onCreateView() method, means it will be invisible fragment. Implement a LruCache object in fragment and have getters and setters to get and set desired objects from LruCache.
Fragments have a characteristics of re-usability in contrast to a simple class. You can find a fragment by its tag.
For example: To add a fragment-
activity.getFragmentManager().beginTransaction().add(fragment, TAG).commitAllowingStateLoss();
and to get a fragment-
activity.getFragmentManager().findFragmentByTag(TAG);
In contrast to normal class Fragment class is removed from memory only when device is in sort of memory. Whenever fragment is null you will have to re-initialize it.
To know more about its implementation details visit this link

What's the difference between 'this' and 'getActivity()'?

I have an idea they both get the 'context' so that functions know how they fit into the scheme of things, is this right?
Any clues for the evolving ape?
this is a reference to the current class you are. It can be an Activity, Fragment, Adapter, View, etc. And when you use it what you are doing is just passing a reference of the current object of that class.
Let's say you are working on a custom View. Anywhere in the code of that view where you call this will be interpreted as the View itself, so the value of this changes depending on what class you are.
The method getActivity() is defined only in the Fragment class and any other class extending Fragment and this method returns and Object of the type Activity.
On Android development is very common mixing those two because most of the applications code is in Activity classes, and calling this on an Activity class will return an Activity object but as you can see they are not the same thing.
You can read more here

Why are we using Context in creating object for other class in Android?

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?

context vs views

can anyone explain the difference between context and views and when do we go for context or view ? In most of the program I find either context or view being passed to certain methods , what is actual need of passing context or view to methods?
This is a strange question. View describes one element of your ui. It can have onClickListeners, properties and so on. But every view is created in some context, usually Activity's context.
Context as itself is something like environment your code is executed in. It has access to ui(if it is an activity), it can contain some global data(application context), and has access to the resources(all of the contexts). Also, context allows you to perform common android operations like broadcasting intents, start activities and services.
So, views should be passed when you want to do something with a particular view.
Context is passed when you need access to resources, global data or ui context, or launch other android components.
We need to understand how View is constructed and what is Context.
View has three constructors, all of which use Context as an argument.
In Activity, if view is inflated programmatically as against in XML, view is inflated a View is by using LayoutInflater.
LayoutInflater takes Context as an argument and internally saves it in a class level field.
LayoutInfater layoutinflater = LayoutInflater.from(this);
where "this" is the Activity instance.
When inflater inflates view i.e. :
inflater.inflate(R.id.some_view, parent, null),
it is internally passing the saved context field to the constructor of View.
View always takes a Context as an argument and this is obvious because views live in some Context which is the Activity.
To answer your question, when context is needed to be passed to a method which itself is in Activity, you can write "this". If method is not in Activity, and you need to pass Context, then remember that View which has taken Context as a parameter, saves the object reference in a class level field. We can get this object reference by writing view.getContext().

Categories

Resources