问题描述
我使用fragmenttabhost的片段。
在一个片段有两个孩子的碎片。
当从子片段更改片段工作的罚款。
但是要一些其他的选项卡时,正在添加和背部使误差
I am using fragmenttabhost for fragments .In one fragment there is are two child fragments .When Changing fragment from child fragment working fine .However when going to some other tab and comming back making error
public class MainActivity extends FragmentActivity implements
OnTabChangeListener {
private FragmentTabHost mTabHost;
private HashMap<String, Stack<Fragment>> mStacks;
private String mCurrentTab;
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.setOnTabChangedListener(this);
mStacks = new HashMap<String, Stack<Fragment>>();
mStacks.put(AppConstants.DEAL_FRAGMENT, new Stack<Fragment>());
mStacks.put(AppConstants.CONN_FRAGMENT, new Stack<Fragment>());
mStacks.put(AppConstants.PROFILE_FRAGMENT, new Stack<Fragment>());
mStacks.put(AppConstants.MORE_FRAGMENT, new Stack<Fragment>());
initializeTabs();
}
private void initializeTabs() {
Bundle b = new Bundle();
b.putString("key", AppConstants.DEAL_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.DEAL_FRAGMENT)
.setIndicator("Deal"),
DealFragment.class, b);
//
b = new Bundle();
b.putString("key", AppConstants.CONN_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.CONN_FRAGMENT)
.setIndicator("Conn"),
ConnFragment.class, b);
b = new Bundle();
b.putString("key", AppConstants.PROFILE_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.PROFILE_FRAGMENT)
.setIndicator("Profile"),
ProfileFragment.class, b);
b = new Bundle();
b.putString("key", AppConstants.MORE_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.MORE_FRAGMENT)
.setIndicator("More"),
MoreFragment.class, b);
}
public void pushFragments(String tag, Fragment fragment,
boolean shouldAnimate, boolean shouldAdd) {
/*if (shouldAdd)
mStacks.get(tag).push(fragment);*/
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (shouldAnimate)
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
}
@Override
public void onTabChanged(String tabId) {
mCurrentTab = tabId;
Log.d("Tabid", "" + mCurrentTab);
if (mStacks.get(tabId).size() == 0) {
if (tabId.equals(AppConstants.DEAL_FRAGMENT)) {
pushFragments(tabId, new DealFragment(), false, true);
} else if (tabId.equals(AppConstants.CONN_FRAGMENT)) {
pushFragments(tabId, new ConnFragment(), false, true);
} else if (tabId.equals(AppConstants.PROFILE_FRAGMENT)) {
pushFragments(tabId, new ProfileFragment(), false, true);
} else if (tabId.equals(AppConstants.MORE_FRAGMENT)) {
pushFragments(tabId, new MoreFragment(), false, true);
}
} else {
pushFragments(tabId, mStacks.get(tabId).lastElement(), false, false);
}
}
public void popFragments() {
Fragment fragment = mStacks.get(mCurrentTab).elementAt(
mStacks.get(mCurrentTab).size() - 2);
mStacks.get(mCurrentTab).pop();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
}
@Override
public void onBackPressed() {
if(((BaseFragment)mStacks.get(mCurrentTab).lastElement()).onBackPressed() == false){
if(mStacks.get(mCurrentTab).size() == 1){
super.onBackPressed(); // or call finish..
}else{
popFragments();
}
}else{
}
}
}
公共类DealFragment扩展BaseFragment实现OnTabChangeListener {
public class DealFragment extends BaseFragment implements OnTabChangeListener {
private FragmentTabHost mTabHost;
private String mCurrentTab;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.id.menu_settings);
mTabHost.setOnTabChangedListener(this);
initializeChildTabHost();
return mTabHost;
}
private void initializeChildTabHost() {
Bundle b = new Bundle();
b.putString("key", AppConstants.LIST_DEAL_FRAGMENT);
mTabHost.addTab(
mTabHost.newTabSpec(AppConstants.LIST_DEAL_FRAGMENT)
.setIndicator(
"List"),
CategoryListFragment.class, b);
//
b = new Bundle();
b.putString("key", AppConstants.CATEGORY_DEAL_FRAGMENT);
mTabHost.addTab(
mTabHost.newTabSpec(AppConstants.CATEGORY_DEAL_FRAGMENT).setIndicator(
"Category"),
CategoryFragment.class, b);
}
@Override
public void onTabChanged(String tabId) {
mCurrentTab = tabId;
Log.d("Current Child Tab", ""+mCurrentTab);
}
}
公共类CategoryListFragment扩展BaseFragment实现OnClickListener {
public class CategoryListFragment extends BaseFragment implements OnClickListener{
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout, container, false);
button = (Button)view.findViewById(R.id.addNewFrag);
button.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
if(v==button){
mActivity.pushFragments(AppConstants.LIST_DEAL_FRAGMENT, new InviteFrndFragment(), true, true);
}
}
}
公共类BaseFragment扩展片段{
公共MainActivity mActivity;
public class BaseFragment extends Fragment { public MainActivity mActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = (MainActivity) this.getActivity();
}
public boolean onBackPressed() {
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}
推荐答案
在您的片段活动中,您可以选择存储pviously加载片段在变量$ P $的参考。例如,
In your fragment activity, you have to store the reference of previously loaded fragment in a variable. For example,
声明一个变量gloabal如下:
Declare a gloabal variable as follows:
Fragment lastFragment = null;
public void pushFragments(String tag, Fragment fragment,
boolean shouldAnimate, boolean shouldAdd) {
// frag
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (shouldAnimate)
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
If(lastFragment==null) {
ft.replace(R.id.realtabcontent, fragment);
lastFragment = fragment;
}
else {
ft.replace(R.id.realtabcontent, LastFragment);
}
ft.commit();
}
这篇关于FragmentTabhost没有活动IllegalStateException异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!