2016-12-29 1 views
0

Mon problème est quelque chose de similaire à this. Je travaille au Skobbler map. J'ai MainActivity qui comprend HomeFragment. Mon HomeFragement inclut le stack of view comme: A->B->C->D. Quand j'atteins le view D, si je back press alors je veux retourner comme: D->-C->B->A.Retour Appuyez sur pour la vue en fragment

Dans mon MainActivity, j'inclure la méthode Fragment dans onCreate:

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

et

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

Dans mon HomeFragment, j'ai méthode onBackPress.

public boolean onBackPressed() {} 

Mais, je suis incapable de revenir final view-first view as homescreen. Que dois-je inclure ici? Que peut-on faire pour résoudre ce problème?

Ajouter 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

quel est le jeu TAG? –

+0

Pouvez-vous ajouter plus sur la pile de vue? –

+0

TAG est rien, 'addToBackStack (chaîne ici)', donc je l'ajoute? Que puis-je ajouter là? –

Répondre

1

Il se sent comme Google a raté cette partie lors de la conception des classes de fragments pour les tablettes. Les fragments peuvent avoir un intérêt à manipuler le bouton de retour, mais ils ne reçoivent aucun type de rappel pour leur faire savoir que le bouton de retour a été pressé.

Voici comment j'ai résolu ce problème.

  • Définir une interface. Je l'ai fait comme une interface interne de l'activité puisque c'est l'activité qui accède aux clients qui l'implémentent.

    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(); 
        } 
    
  • fragments qui ont un intérêt pour le bouton retour permet de mettre en œuvre cette interface

    public class MyFragment extends Fragment implements MyActivity.HandlesOnBackPressed { 
    
        ... 
    
        @Override 
        public boolean onBackPressed() { 
    
         ... 
    
  • Lorsque vous faites la transaction fragment:

    1. Donnez le fragment HandlesOnBackPressed une étiquette unique lorsque vous faites la transaction (replace etc.)
    2. Lorsque vous poussez la transaction vers la pile arrière, attribuez à cette pile de retour la même étiquette.

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

    Le fait que je le nom de classe n'a aucune incidence sur la logique. La seule exigence est que l'étiquette de fragment et l'étiquette d'entrée de pile arrière soient les mêmes.

  • Et voici la sauce secrète.Remplacer onBackPressed dans l'activité:

    @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, pourquoi me fais-tu faire ce genre de choses?