问题
I am working with the support library ActionBar because I'm using an older minimum SDK. In the activity, I am using FragmentTabHost because I have 3 tabs. The ActionBar also has a SearchView, so when a search is made, the 3rd tab is switched out with the results of the search.
I am able to get intput from the SearchView, but I am unable to switch out the 3rd tab when I have a search result. I am using this as an example: Dynamically changing the fragments inside a fragment tab host?
My problem is that when I try to get a reference to the 3rd tab, and I use getSupportFragmentManager().findFragmentByTag() , the fragment being returned is always null.
My base container that helps in swapping multiple fragments in a tab:
public class BaseContainerFragment extends Fragment{
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
//Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
A container that extends BaseContainerFragment
public class LibraryContainerFragment extends BaseContainerFragment {
private boolean mIsViewInited;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("test", "tab 1 oncreateview");
return inflater.inflate(R.layout.container_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("test", "tab 1 container on activity created");
if (!mIsViewInited) {
mIsViewInited = true;
initView();
// setRetainInstance(true);
}
}
private void initView() {
Log.e("test", "tab 1 init view");
replaceFragment(new LibraryFragment, false);
}
}
The xml used to switch out fragments (container_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
My main activity:
public class BookSetup extends ActionBarActivity {
// For accessing SlidingMenu library
private SlidingMenu slidingMainMenu;
private FragmentTabHost mTabHost;
private SlidingMenu slidingContextMenuFavourites;
private SlidingMenu slidingContextMenuMyPrayerBook;
private SlidingMenu slidingContextMenuLibrary;
private android.support.v4.app.FragmentManager fragmentManager;
LibraryContainerFragment libraryContainerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Settings for the clickable top-left button in the action bar
android.support.v7.app.ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(false);
bar.setHomeButtonEnabled(true);
bar.setIcon(R.drawable.main_menu);
// Setting up tabbed navigation
bar.setDisplayShowTitleEnabled(false);
// Setting up tabs
fragmentManager = getSupportFragmentManager();
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, fragmentManager, R.id.realtabcontent);
// Add tabs
mTabHost.addTab(mTabHost.newTabSpec("Favourites").setIndicator(getString(R.string.favourites) ),
FavouritesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("My Book").setIndicator(getString(R.string.my_book) ),
MyBookFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Library").setIndicator(getString(R.string.library) ),
LibraryContainerFragment.class, null);
mTabHost.setCurrentTab(2);
//mTabHost.
// Creates a sliding animation when activity is started
overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu_with_search_context_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
final MenuItem searchMenuItem = menu.findItem(R.id.action_search);
searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchMenuItem);
//searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
//searchView.requestFocus();
searchView.requestFocusFromTouch();
//searchView.setIconifiedByDefault(true);
// Listener for the search input found in the action bar, when the magnifying glass is
// clicked
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// Activated when a search string is submitted
@Override
public boolean onQueryTextSubmit(String query) {
// TODO : query is the text from the search view after you clicked search
if(query != null){
// If results are found, then switch the fragments
if(!sectionsFound.isEmpty()){
// Initialize the search fragment and send bundles of data to it
SearchLibraryResultFragment fragment = new SearchLibraryResultFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("values",
(ArrayList<? extends Parcelable>) sectionsFound);
bundle.putStringArrayList("names", sectionNames);
fragment.setArguments(bundle);
mTabHost.setCurrentTab(2);
libraryContainerFragment = (LibraryContainerFragment)fragmentManager.findFragmentByTag("Library");
((BaseContainerFragment) libraryContainerFragment.getParentFragment() ).replaceFragment(fragment,true );
return true;
}
});
}
}
This is the line that always returns null in BookSetup.java:
((BaseContainerFragment) libraryContainerFragment.getParentFragment() ).replaceFragment(fragment,true );
回答1:
You are using the ChildFragmentManager
to replace your Fragments
right?
That might be your problem, instead of
libraryContainerFragment = (LibraryContainerFragment)fragmentManager.findFragmentByTag("Library");
use
libraryContainerFragment = (LibraryContainerFragment) getChildFragmentManager().findFragmentByTag("Library");
来源:https://stackoverflow.com/questions/25629048/findfragmentbytag-is-returning-null-when-working-with-getsupportfragmentmanage