所以我只有一个活动是MainActivity
。所有其他部分都是碎片形式。
MainActivity -> AnnouncementsFragment-> AnnouncementDetailFragment
我想要发生的是当用户按两次后退键时,他返回上一个片段而不是关闭它。
MainActivity <- AnnouncementsFragment <- AnnouncementDetailFragment
根据研究,我发现这段代码很遗憾不适用于我的实例。顺便说一下,我正在使用导航边栏。
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if(getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
布局/ content_main
基本上,我在content_main.xml
中所做的只是可替换数据的容器。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.techdepot_ph.maco.iannounce.MainActivity"
tools:showIn="@layout/app_bar_main">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/sti_overlay_bg"
android:layout_marginTop="84dp"
android:id="@+id/imageView4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/root_layout">
</FrameLayout>
</RelativeLayout>
启动片段
我在启动片段时所做的是用户点击导航栏中的项目
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_message) {
getSupportActionBar().setTitle("Create Announcement");
fragment = new CreateMessageFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.root_layout, fragment);
ft.commit();
} else if (id == R.id.nav_announcements) {
getSupportActionBar().setTitle("Announcements");
fragment = new AnnouncementsFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.root_layout, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 0 :(得分:0)
在提交新片段之前,请将之前的片段添加到backstack。
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
在这里阅读更多内容。
答案 1 :(得分:0)
加载片段时将它们添加到backstack
fragment = new CreateMessageFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.root_layout, fragment);
ft.addToBackStack(null)
ft.commit();
并在您的活动中按
private Boolean exit = false;
@Override
public void onBackPressed() {
if (exit) {
if (getSupportFragmentManager().getBackStackEntryCount() > 0 ){
getSupportFragmentManager().popBackStack();
}
else
super.onbackpress();
}
else {
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}