gpt4 book ai didi

java - 返回之前的 fragment

转载 作者:行者123 更新时间:2023-12-01 12:13:49 26 4
gpt4 key购买 nike

我正在使用 fragment 制作一个 Android 应用程序,但我以前没有使用过 fragment 的经验。

我有一个主 FragmentActivity,我在其中加载主视图并调用我的 fragment 类:

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main_view);
FragmentTransaction FT = getFragmentManager().beginTransaction();

if (findViewById(R.id.fragmentAction) != null) {

FT.replace(R.id.fragmentAction, MainMenuFragment.newInstance(
getString(R.string.main_menu), getApplicationContext()));

}

FT.addToBackStack(null);
FT.commit();
}
@Override
public void onBackPressed() {

}

这是我的 fragment 类,当我按下按钮调用其他 fragment 时:

public class MainMenuFragment extends Fragment {

private static String my_description = "";
private static Context my_context = null;

public static MainMenuFragment newInstance(String description,
Context context) {
my_description = description;
my_context = context;
MainMenuFragment f = new MainMenuFragment();
return f;
}

public MainMenuFragment() {

}



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = null;

if (my_description.compareTo(getString(R.string.main_menu)) == 0) {

view = inflater.inflate(R.layout.main_menu, container, false);

}

return view;
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

if (my_description.compareTo(getString(R.string.main_menu)) == 0) {

Button new_user = (Button) getView().findViewById(
R.id.button_newUser);
new_user.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

FragmentTransaction FT = getFragmentManager()
.beginTransaction();
FT.replace(R.id.fragmentAction, LdapFragment.newInstance(
getString(R.string.new_user), my_context));
FT.commit();

}
});



}

}


}

我的问题是:当我按回键时如何返回到之前的 fragment ?我在 FragmentActivity 中添加 onBackPressed 函数并捕获该事件,但我该怎么办?

最佳答案

使用此代码

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
this.finish();
} else {
getSupportFragmentManager().popBackStack();
removeCurrentFragment();
}
}



public void removeCurrentFragment() {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
Fragment currentFrag = getSupportFragmentManager().findFragmentById(
R.id.fragment_container);
setFragName("NONE");
if (currentFrag != null) {
setFragName(currentFrag.getClass().getSimpleName());
}
if (currentFrag != null) {
transaction.remove(currentFrag);
}
transaction.commit();

}

关于java - 返回之前的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27126469/

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