2016-12-29 3 views
0

Mein Problem ist etwas ähnliches wie this. Ich arbeite in Skobbler map. Ich habe MainActivity, die HomeFragment enthalten. Meine HomeFragement enthält die stack of view wie: A->B->C->D. Wenn ich die view D erreiche, wenn ich back press dann möchte ich zurückgehen wie: D->-C->B->A.Zurück Drücken Sie für die Ansicht in Fragment

In meinem MainActivity, ich schließe die Fragment in onCreate Methode:

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

und

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

In meinem HomeFragment, ich habe onBackPress Methode.

public boolean onBackPressed() {} 

Aber ich bin nicht in der Lage final view-first view as homescreen zurück zu kommen. Was soll ich hier angeben? Was kann getan werden, um dieses Problem zu lösen?

Add-Code

<?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> 
+0

Was ist das TAG-Set? –

+0

Können Sie mehr über den Stapel der Ansicht hinzufügen? –

+0

TAG ist nichts, 'addToBackStack (String hier)', also füge ich das hinzu? Was kann ich dort hinzufügen? –

Antwort

1

Es fühlt sich an wie Google diesen Teil verpasst, wenn die Fragment Klassen für Tabletten zu entwerfen. Fragmente haben möglicherweise ein Interesse daran, die Zurück-Schaltfläche zu bearbeiten, aber sie erhalten keine Art von Rückruf, um sie wissen zu lassen, dass die Zurück-Schaltfläche gedrückt wurde.

So habe ich dieses Problem gelöst.

  • Definieren Sie eine Schnittstelle. Ich tat dies als eine innere Schnittstelle der Aktivität, da es die Aktivität ist, die auf die Clients zugreift, die sie implementieren.

    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(); 
        } 
    
  • Fragmente, die ein Interesse an der Zurück-Taste haben diese Schnittstelle implementieren

    public class MyFragment extends Fragment implements MyActivity.HandlesOnBackPressed { 
    
        ... 
    
        @Override 
        public boolean onBackPressed() { 
    
         ... 
    
  • Wenn Sie das Fragment Transaktion tun:

    1. das HandlesOnBackPressed Fragmente einen eindeutigen Tag geben, wenn Sie tun die Transaktion (replace usw.)
    2. Wenn Sie die Transaktion auf den Backstack schieben, geben Sie diesem Backstack-Eintrag das gleiche Tag.

      MyFragment fragment = MyFragment.newInstance(... 
          fragmentManager 
            .beginTransaction() 
            .replace(R.id.fragment_container, fragment, "MyFragment") 
            .addToBackStack("MyFragment") 
            .commit(); 
      

    Die Tatsache, dass ich den Namen der Klasse hat auf der Logik keine Lager verwendet. Die einzige Voraussetzung ist, dass das Fragment-Tag und das Back-Stack-Entry-Tag identisch sind.

  • Und hier ist die geheime Soße.Außer Kraft setzen onBackPressed der Tätigkeit:

    @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 
         } 
    
        } 
    

Google, warum Sie mir dieses Zeug machen tun?

Verwandte Themen