问题
Searched and saw a few posts for the similar question, but no working solution. Post here to see if someone have solved it.
Having FragmentTabHost in a fragment, and trying to get current tab's content fragment.
in mTabHost.setOnTabChangedListener's onTabChanged(String tabTag) the frgmt = getChildFragmentManager().findFragmentByTag(tabTag); returns null.
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabTag) {
Fragment frgmt = getChildFragmentManager().findFragmentByTag(tabTag);
}
});
In FragmentTabHost, its onTabChanged() calls doTabChanged() which instantiates the content fragment and add with FragmentTransaction, or attach to it if the fragment exist.
I guess when FragmentTabHost::onTabChanged() calls into the listener's onTabChanged(), the fragment is still not ready in the FragmentManager by getChildFragmentManager().
Is there a way the get the mLastTab from the FragmentTabHost? It should have the current content fragment.
Thanks for the help!
@Override
public void onTabChanged(String tabId) {
if (mAttached) {
FragmentTransaction ft = doTabChanged(tabId, null);
if (ft != null) {
ft.commit();
}
}
if (mOnTabChangeListener != null) {
mOnTabChangeListener.onTabChanged(tabId);
}
}
private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
TabInfo newTab = null;
for (int i=0; i<mTabs.size(); i++) {
TabInfo tab = mTabs.get(i);
if (tab.tag.equals(tabId)) {
newTab = tab;
}
}
if (newTab == null) {
throw new IllegalStateException("No tab known for tag " + tabId);
}
if (mLastTab != newTab) {
if (ft == null) {
ft = mFragmentManager.beginTransaction();
}
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(mContext,
newTab.clss.getName(), newTab.args);
ft.add(mContainerId, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
}
return ft;
}
回答1:
The problem as you can see is ft.commit in OntabChanged() and then we call
if (mOnTabChangeListener != null) {
mOnTabChangeListener.onTabChanged(tabId);
}
the tab fragment still not created hence throws null pointer exception when we try to get the instance of Tabfragement inside OnTabChnaged callback.
My Solution : we should use the way two fragments communicate with each other using activity.
Here is the activity
public class MainActivity extends ActionBarActivity implements OnHeadlineSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void OnTabCompletelyChanged(String tag) {
((MainActivityFragment)getSupportFragmentManager().findFragmentById(R.id.fragment)).OnTabChanged(tag);
}
}
MainFragment Holding the Tabs
public class MainActivityFragment extends Fragment {
private FragmentTabHost mTabHost;
private FragmentTab tab;
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
FragmentTab.class, null);
return v;
}
public void OnTabChanged(String tag) {
tab = (FragmentTab)getChildFragmentManager().findFragmentByTag(tag); // Not null
}
}
FragmentTab:
public class FragmentTab extends Fragment {
private OnHeadlineSelectedListener mCallback;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onStart() {
super.onStart();
mCallback.OnTabCompletelyChanged(this.getTag());
}
public String getString(){
return "hello";
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
TextView tv = (TextView) v.findViewById(R.id.text);
tv.setText(this.getTag() + " Content");
return v;
}}
回答2:
You should try some thing like this:-
FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();
I hope it may help you
来源:https://stackoverflow.com/questions/30293549/how-to-get-the-content-fragment-at-ontabchangedstring-tabtag