返回按片段中的视图

时间:2016-12-29 11:51:05

标签: android android-fragments onbackpressed

我的问题与this类似。我在Skobbler map工作。 我有MainActivity,其中包含HomeFragment。我的HomeFragement包含stack of view类似:A->B->C->D。当我到达view D时,如果我back press,那么我想回去:D->-C->B->A

在我的MainActivity中,我在Fragment方法中添加了onCreate

homeFragment = HomeFragment.newInstance();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.llMainActivityContainer, homeFragment)
            .addToBackStack(TAG)
            .commit();

@Override
public void onBackPressed() {
    if (!homeFragment.onBackPressed()) {
        int count = getSupportFragmentManager().getBackStackEntryCount();
        if (count > 0) {
            getSupportFragmentManager().popBackStack();
            return;
        }
        super.onBackPressed();
    }
}

在我的HomeFragment中,我有onBackPress方法。

public boolean onBackPressed() {}

但是,我无法从final view返回first view as homescreen。我应该在这里包括什么?可以做些什么来解决这个问题?

添加代码

<?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"
android:id="@+id/rlHomeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">


<com.skobbler.ngx.map.SKMapViewHolder
    android:id="@+id/skmVHHome"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<RelativeLayout
    android:id="@+id/chess_board_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/map_background" />

<include layout="@layout/layout_driving_direction_info" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fabDrivingDirections"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/fabCurrentLoc"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    app:backgroundTint="@color/blue_panel_day_background" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fabCurrentLoc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="150dp"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    app:backgroundTint="@color/blue_panel_day_background" />


<include
    layout="@layout/layout_drive_info"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

在设计平板电脑的片段类时,感觉Google错过了这一部分。片段可能对处理后退按钮感兴趣,但是他们没有收到任何类型的回调,让他们知道按下后退按钮。

这就是我解决这个问题的方法。

  • 定义界面。我这样做是活动的内部接口,因为它是访问实现它的客户端的活动。

        public interface HandlesOnBackPressed {
    
            /**
             * Callback for back button pressed event.
             * @return true if the back press was handled so the activity doesn't need to.
             */
            boolean onBackPressed();
        }
    
  • 对后退按钮感兴趣的片段将实现此接口

    public class MyFragment extends Fragment implements MyActivity.HandlesOnBackPressed {
    
        ...
    
        @Override
        public boolean onBackPressed() {
    
            ...
    
  • 执行片段事务时:

    1. 执行交易时HandlesOnBackPressed片段为replace片段提供唯一标记( MyFragment fragment = MyFragment.newInstance(... fragmentManager .beginTransaction() .replace(R.id.fragment_container, fragment, "MyFragment") .addToBackStack("MyFragment") .commit(); 等。)
    2. 当您将事务推送到后台堆栈时,请将该堆栈条目指定为相同的标记

      onBackPressed
    3. 我使用类名的事实与逻辑无关。唯一的要求是fragment标签和后栈条目标签是相同的。

    4. 这是秘密酱。覆盖活动中的 @Override public void onBackPressed() { FragmentManager fragmentManager = getSupportFragmentManager(); int count = fragmentManager.getBackStackEntryCount(); if (count > 0) { FragmentManager.BackStackEntry topBackStackEntry = fragmentManager.getBackStackEntryAt(count - 1); if (topBackStackEntry != null) { String tag = topBackStackEntry.getName(); if (tag != null) { // since super.onBackPressed() hasn't been called yet, // the top back stack entry should match with the current fragment Fragment fragment = fragmentManager.findFragmentByTag(tag); if (fragment instanceof HandlesOnBackPressed) { if (((HandlesOnBackPressed) fragment).onBackPressed()) { // the fragment handled the back press, // so don't call super.onBackPressed() return; } } } } } DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawerLayout != null && drawerLayout.isDrawerOpen(GravityCompat.START)) { drawerLayout.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); // pops back stack, onBackStackChanged() is called } }

      {{1}}
谷歌,你为什么要让我这样做呢?