gpt4 book ai didi

java - MainActivity 到 Fragment,然后从该 Fragment 返回到 MainActivity

转载 作者:行者123 更新时间:2023-12-02 04:32:21 25 4
gpt4 key购买 nike

Solve by using getActivity()
我有这个 MainActivity.java 和 RepeatEntry.java

在我的 MainActivity 中,我有这个代码来实现 RepeatEntry ui

//i did hide two linear layout here with buttons and edittext inside it ,using the following method 
hideTwoLinearLayout();
showCategoryContainerLayout();
Fragment fragment = new RepeatEntry();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//category_cont is a linear layout container for my fragment
ft.replace(R.id.category_cont, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();

在我的 RepeatEntry.java 示例代码中

Button k = (Button) v.findViewById(R.id.button);
k.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent = new Intent(getActivity(),MainActivity.class);
// startActivity(intent);

// if i use popBackStack and also remove the code for intent , i cannot show what i hide
//note i have a method inside mainactivity to showTwoLinearLayout()
getFragmentManager().popBackStack();
}
});

现在我的问题是,除了使用 Intent 返回 MainActivity View 之外,我还有其他选择吗

注意:已编辑

最佳答案

您可以将交易添加到后台堆栈,然后通过弹出后台堆栈进行反向操作,代码在这里

 Fragment fragment = new RepeatEntry();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//category_cont is a linear layout container for my fragment
ft.replace(R.id.category_cont, fragment).addToBackStack("tag");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();

要返回到 Activity View ,请调用此函数来弹出后退堆栈

FragmentManager fm = getFragmentManager();
fm.popBackStack();

您还可以使用标签来弹出特定交易

fm.popBackStack("tag");

关于java - MainActivity 到 Fragment,然后从该 Fragment 返回到 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283344/

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