gpt4 book ai didi

java - 如何通过单击抽屉导航 Activity 中的后退按钮从其他 fragment 返回特定 fragment

转载 作者:行者123 更新时间:2023-11-30 10:39:57 26 4
gpt4 key购买 nike

我知道之前在这里讨论过这个主题,但没有一个对我的代码有用。我做了什么:

在 Home.java(抽屉导航主 Activity )中将其用于抽屉导航功能

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;

if (id == R.id.nav_breakfast) {
fragmentClass = Breakfast.class;
} else if (id == R.id.nav_lunch) {
fragmentClass = Lunch.class;
} else if (id == R.id.nav_wallet) {
fragmentClass = Wallet.class;
} else if (id == R.id.nav_history) {
fragmentClass = History.class;
} else if (id == R.id.nav_reset) {
fragmentClass = ResetPass.class;
} else if (id == R.id.nav_send) {
fragmentClass = ContactUs.class;
} else if (id == R.id.nav_help) {
fragmentClass = Help.class;
} else if (id == R.id.nav_about) {
fragmentClass = AboutUs.class;
}

try {
assert fragmentClass != null;
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
assert drawer != null;
drawer.closeDrawer(GravityCompat.START);

return true;
}

通过单击抽屉导航项目选择打开每个声明的 fragment 。另一方面,我还创建了另一个 fragment ,该 fragment 设置为在 Home Activity 实际开始时默认打开

 if (savedInstanceState == null) {
Fragment fragment = null;
Class fragmentClass;
fragmentClass = AllProduct.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

在 onBackPressed() 上我使用了这个:

@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
else if (doubleBackToExitPressedOnce) {
super.onBackPressed();
}
else {
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}

所有的 fragment Activity 都是这样编码的:

public class AllProduct extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_product, container, false);

return view;
}

@Override
public void onResume() {
super.onResume();
getActivity().setTitle(R.string.app_name);
}

现在一切正常,只有一个问题。假设我想从抽屉导航中打开 Fragment [1](不是上面提到的默认 fragment ),然后再次打开抽屉导航并打开 Fragment [2],然后是 Fragment [3]。现在假设我在 Fragment [3] 中,按下后退按钮我直接想转到上面提到的默认 fragment 。它首先转到 fragment [2]、 fragment [1],然后是默认 fragment 。但我想要无论何时何地,在哪个 fragment 中,我都必须通过按下后退按钮返回到默认 fragment 。

最佳答案

就用

getSupportFragmentManager()
.beginTransaction()
.replace(...)
.addToBackStack("tag").commit()

并删除所有其他代码,尤其是 onBackPressed() 中的代码

关于java - 如何通过单击抽屉导航 Activity 中的后退按钮从其他 fragment 返回特定 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104250/

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