Alternative of findFragmentById for getting a reference of already open fragment - android

Im trying to tell a fragment to change a certain view's visibility from the activity, but I dont know how to get a reference to that fragment.
What I found is
findFragmentById(R.id.asdasd)
But my fragment is not inflated from an XML layout so it doesnt have such an ID (I guess?)
So how can I reference this fragment in another way?
Here is how I add the fragment from the activity:
public void addFragment(Fragment fragment) {
String fragmentClassName = ((Object) fragment).getClass().getSimpleName();
FragmentTransaction t = getFragmentManager().beginTransaction();
t.replace(R.id.fragment_container, fragment, fragmentClassName);
t.addToBackStack(fragmentClassName);
t.commit();

Since you already set the tag in your fragment you can use it to find the fragment by its tag using the Fragment Manager.
sample:
String fragmentClassName = The_Class_Name_Of_Fragment.getClass().getSimpleName();
YourFragment fragment = getFragmentManager().findFragmentByTag(fragmentClassName);

You should call findFragmentById(R.id.fragment_container) to reference your Fragment.

Related

Android activity with ListView open fragment on item click

I have an Activity with ListView, when clicking on a list item opens a new fragment.
Do I need to create a new fragment every time like this?
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,new MyFragment());
Or will be enough to create a fragment once and then use it?
in activity:
MyFragment myFragment = new MyFragment();
......
in onItemClickListener:
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,myFragment);
It depends on your case. In most cases every list item opens a different fragment (with different data). Then you have to make a static newInstance(Data mySerializableData) method in your fragment, use default constructor inside of it, pass data over Fragment arguments See DetailFragment and use fragmentTransaction.replace() in your activity to add this fragment.
When you dont want your fragment to be changed you can create it only once as you say but there is no need of adding it on every item click. So one creation and only one add.
No, you don't need to create it every time. First, instead of using "add", use "replace". If there is no fragment in fragment manager, your fragment will be added, instead it will be replaced. If you use "add", then you could accidentally add more than one fragment.
You should check for the fragment in the fragment manager and call methods for content updates.
Example:
myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
if (myFragment == null) {
myFragment = MyFragment.getInstance(someDataIfNeeded);
fragmentManager.beginTransaction().replace(R.id.my_fragment, myFragment).commit();
} else {
myFragment.updateFragmentContent(someData);
}
check instance of that fragment everytime like this-
In your fragment class -
public static final MyFragment newInstance()
{
MyFragment f = new MyFragment();
return f;
}
and in your activity when you want to create fragment -
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,MyFragment.newInstance());
this is well n good manner...

Run function in another fragment

I want to run a function from main activity, but function is another fragment. This is my code
FragmentManager fm = getSupportFragmentManager();
ConversationFragment fragment = (ConversationFragment)fm.findFragmentById(R.id.container);
fragment.addMessageToList("ok");
I placed this code to onCreate in MainActivity, and this is the addMessageToList function in fragment:
public void addMessageToList(String message) {
Log.w("Step 2",message);
}
But my app is crashing. Here logcat:
How can I fix it ?
You were using findFragmentById() when you had no fragment with that id on your Activity layout. When replacing a fragment in your container, add a tag to it. Like this:
fragmentTransaction.replace(R.id.container, frgObj,"ConversationFragment");
Then, use findFragmentByTag("ConversationFragment") to have your Fragment and execute methods with it.

Changing Fragments inside Activity through onListItemClick()

I have this Activity which at first shows a Fragment with a list of elements. This works perfectly with this code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_act);
if(null == savedInstanceState)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListFragment glfragment = new ListFragment();
fragmentTransaction.add(R.id.listfrag1, glfragment);
fragmentTransaction.commit();
}
}
Well I have a ListFragment and a DetailFragment. But I don't know how to do the transition when I click an element of the list. I know the fragmentTransaction.replace(), but I don't know WHEN to call it.
I thought I should use the OnListItemClick() inside the ListFragment, but I don't know how to use the FragmentManager inside the Fragment and not in the main Activity... Also I want to "export" some data to the DetailFragment as if it was a Intent, but it's not.
To use the fragment manager inside your Fragment, simply call
getActivity().getFragmentManager() instead of getFragmentManager(). Implementing this in your OnItemClickListener should suffice.
What I would do is:
Define an interface with one method listItemSelected() with as an argument the id of the selected item
Let your activity implement this interface
In the onAttach of your list fragment, take the activity and keep it as a member variable, cast to the interface type. Make sure that in the onDetach you dereference it.
In your onListItemClick, call this method on your activity
In the activity, you can now do a new fragmenttransaction, this time you need to replace instead of add the fragment
To create your detail fragment with the correct argument (the id), use the method described here.
This should normally work fine.

how to identify the current fragment?

My question is not easy to describe, but I will do my best:
On my tablet-app I have one activity with a listFragment A on left side and a detailFragment B on right side. So when I click an item on the list, the detailFragment shows the proper details of the chosen (list) item.
Now when I click a button on my detailFragment B. the fragment gets swapped with a new Fragment from type infoFragment. The listFragment on left side stays as it is.
So now, when I click another item on the List, I want the infoFragment to vanish and get a detailFragment once again.
My problem is, that i need some kind of check if currently there is an infoFragment or a detailFragment displayed. So that I can either just refresh the detailFragment OR stop the infoFragment and build a new detailFragment.
idea:
if ( //detailFragment is active ) {
updateContent();
}
else {
FragmentManager.buildDetailFragment();
}
have been fiddling for hours now, any help is appreciated!!
How can i figure it out whether there is a detailFragment or listFragment displayed?
edit:
i change my detailFragment with the infoFragment here:
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.details_fragment);
fragment = new InfoFragment();
fm.beginTransaction()
.replace(R.id.details_fragment, fragment)
.commit();
When you add a Fragment to your fragment manager with a FragmentTransaction you can specify a key value. You can then findFragmentByTag which will determine if the Fragment with that key value has been added to the fragment manager.
So long as you are not using a ViewPager or some other structure where multple fragments are added at once, searching for whether your fragment manager contains a fragment by tag will let you know which is currently displayed. You can then use the results of that search to update the fragment since the result is a reference to the fragment itself.
This means you can pass data from the FragmentActivity to the fragment directly by calling any publicly accessable fragment methods. For example
Fragment displayedFragment = fragmentManager.findFragmentByTag(TAG);
if(displayedFragment != null){ //null if no fragment with tag value
displayedFragment.updateList() //public method within fragment
}
MyFragment myFragment = (MyFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");
if (myFragment.isVisible()) {
// add your code here
}
From Here
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.content_id);
now we can get the fragment name by getClass
fragment.getClass().getSimpleName()
You can get class of fragment and check which one it exactly is by calling getClass().

Find Fragment by tag name in Container

I have a relative layout and i am adding fragment in that relative layout.
Like this
HomeFragment mHomeFragment = new HomeFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
if(mHomeFragment!=null&& mHomeFragment.isAdded()){
fragmentTransaction.show(mHomeFragment);
}else {
fragmentTransaction.add(R.id.order_container,mHomeFragment,"Search");
}
fragmentTransaction.commit();
I also have a text view where i have to display the name of fragment i added in the relative layout.
Like search in case.
How to get the name of fragment added in my relative layout suppose with
id = R.id.order_container
you can retrieve a Fragment by tag through
Fragment fragment = getFragmentManager().findFragmentByTag("yourstringtag");
then you can check if your fragment is instace of HomeFragment
if (fragment instanceof HomeFragment) {
}
Fragment fragment = getFragmentManager().findFragmentById(R.id.order_container);
String tag = (String) fragment.getTag();
If you want to get the tag of a fragment you can use the getTag() method. This probably isn't going to be the nicest way to achieve what you are looking to do.
http://developer.android.com/reference/android/app/Fragment.html#getTag()
If you have several fragments and you will change them around, consider using an adapter.
You want the name?? You can get your fragment using the ID and doing:
fragment.getClass().getName();
is it what you want?
Or you can use:
fragment.getTag();
it's returns the tag name of the fragment, if specified.
You can use findFragmentById in FragmentManager
getFragmentManager().findFragmentById(R.id.order_container)
This is late but I hope this will be helpful for someone.
To find fragment by tag we can do this:
String tag=getSupportFragmentManager().getFragments().get(index).getTag();
SecondFragment f = (SecondFragment) getSupportFragmentManager().findFragmentByTag(tag);
getSupportFragmentManager().findFragmentById(R.id.container).getClass().getName();

Categories

Resources