gpt4 book ai didi

android - Android 中 TabHost 中每个 fragment 的自定义返回堆栈

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:35 24 4
gpt4 key购买 nike

由于 TabActivity 已被弃用,我尝试用开发者 android 网站中已经提到的 Fragments 替换它。但是正如你们已经知道的那样,用 fragment 替换选项卡存在问题,因为只有一个 Activity ,即 fragment Activity ,每个 fragment 都没有返回堆栈,正如您在其他 SO 问题中看到的那样的开发人员表示,您需要将自己的自定义返回堆栈作为一种解决方案进行管理。

我已经创建并实现了我自己的自定义返回堆栈,如下所示,它工作得非常完美,我可以跟踪每个选项卡中的每个 fragment 。

标签1

    Fragment1 > Fragment2 > Fragment3

选项卡2

    Fragment5 > Fragment6

根据上面的导航,如果我从 Fragment1 导航到 Fragment 3,然后如果我将选项卡更改为 Tab2 并返回 Tab1,我仍然可以看到 Fragment3,我什至可以使用我的自定义导航回 Fragment1返回堆栈。

但问题是,当我返回 Tab1 并看到 Fragment3 时,Fragment3 被重新创建,我看不到在将选项卡更改为 Tab2 之前留下的更改。

这是我的自定义返回堆栈;

public static HashMap<String, Stack<Fragment>> customBackStack;

public static Stack<Fragment> simpleStack;
public static Stack<Fragment> contactStack;
public static Stack<Fragment> customStack;
public static Stack<Fragment> throttleStack;
public static Stack<Fragment> homeStack;

customBackStack = new HashMap<String, Stack<Fragment>>();

homeStack = new Stack<Fragment>();
simpleStack = new Stack<Fragment>();
contactStack = new Stack<Fragment>();
customStack = new Stack<Fragment>();
throttleStack = new Stack<Fragment>();

customBackStack.put("home", homeStack);
customBackStack.put("simple", simpleStack);
customBackStack.put("contacts", contactStack);
customBackStack.put("custom", customStack);
customBackStack.put("throttle", throttleStack);

这里是 onTabChanged 方法;

        public void onTabChanged(String tabId) {
TabInfo newTab = mTabs.get(tabId);
if (mLastTab != newTab) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
if (!customBackStack.get(tabId).isEmpty()) {
Fragment fragment = customBackStack.get(tabId).pop();
customBackStack.get(tabId).push(fragment);
ft.replace(mContainerId, fragment);
}
} else {
if (!customBackStack.get(tabId).isEmpty()) {
Fragment fragment = customBackStack.get(tabId).pop();
customBackStack.get(tabId).push(fragment);
ft.replace(mContainerId, fragment);
}
}
}

mLastTab = newTab;
ft.commit();
mActivity.getSupportFragmentManager().executePendingTransactions();
}
}

这里是 onBackPressed;

    public void onBackPressed() {

Stack<Fragment> stack = customBackStack.get(mTabHost.getCurrentTabTag());

if (stack.isEmpty()) {
super.onBackPressed();
} else {

Fragment fragment = stack.pop();

if (fragment.isVisible()) {
if (stack.isEmpty()) {
super.onBackPressed();
} else {
Fragment frg = stack.pop();
customBackStack.get(mTabHost.getCurrentTabTag()).push(frg);

transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right,
R.anim.slide_in_right, R.anim.slide_out_left);

transaction.replace(R.id.realtabcontent, frg).commit();
}
} else {
getSupportFragmentManager().beginTransaction().replace(R.id.realtabcontent, fragment).commit();
}
}

}

因此,自定义返回堆栈工作正常,除了更改选项卡之前的最后一个 fragment 在返回选项卡后重新创建,它不像选项卡 Activity 中那样恢复。

知道如何解决吗?

编辑

您可以在此处找到我的示例应用程序作为解决此问题的方法。

GitHub

最佳答案

我将 googles 示例 tablistener 修改为以下内容:

public static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener {
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
public SherlockFragment mFragment;
private final Stack<SherlockFragment> mStack;

public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null, null);
}

public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle args, Stack<SherlockFragment> stack) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
mStack = stack;

// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
// we need to reattach ALL fragments thats in the custom stack, if we don't we'll have problems with setCustomAnimation for fragments.
if (mFragment == null) {
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.replace(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
if(mStack != null) {
for(SherlockFragment fragment: mStack) {
ft.attach(fragment);
}
}
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
if(mStack != null) {
for(SherlockFragment fragment: mStack) {
if(fragment!= null && !fragment.isDetached()) {
ft.detach(fragment);
}
}
}
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}

我覆盖的 onKeyDown 方法如下:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
int index = getSupportActionBar().getSelectedNavigationIndex();
Stack<SherlockFragment> stack = null;
SherlockFragment fragment = null;
String rootTag = null;
switch(index) {
case 0:
stack = mWatersTabListener.mStack;
fragment = mWatersTabListener.mFragment;
rootTag = mWatersTabListener.mTag;
break;
case 1:
stack = mHarborTabListener.mStack;
fragment = mHarborTabListener.mFragment;
rootTag = mHarborTabListener.mTag;
break;
}

if(stack.isEmpty()) {
return super.onKeyDown(keyCode, event);
} else {
SherlockFragment topFragment = stack.pop();
FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction ft = fragmentManager.beginTransaction();

ft.setCustomAnimations(R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);

if(topFragment != null && !topFragment.isDetached()) {
ft.detach(topFragment);
}

if(stack.isEmpty()) {
ft.replace(android.R.id.content, fragment, rootTag);
ft.commit();
return true;
} else {
SherlockFragment nextFragment = stack.peek();
ft.replace(android.R.id.content, nextFragment);
ft.commit();
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}

需要注意的重要事项是,您必须在选中时附加自定义堆栈中的所有 fragment ,并且在未选中时也必须分离所有 fragment 。如果在理解代码 fragment 方面有问题,请提问。

关于android - Android 中 TabHost 中每个 fragment 的自定义返回堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12854105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com