2017-02-08 1 views
0

Donc, je n'ai qu'une seule activité qui est le MainActivity. Toutes les autres parties sont sous la forme de fragments.Retour au fragment précédent

Sidebar Announcement List Announcement Detail Donc, si nous allons à Annonces, au fond, ce qui va arriver est

MainActivity -> AnnouncementsFragment-> AnnouncementDetailFragment 

Ce que je voulais arriver est lorsque l'utilisateur appuie sur la touche à deux reprises, il retourne à le Fragment précédent au lieu de le fermer. Comme recherche, j'ai trouvé ce code qui malheureusement ne fonctionne pas avec mon instance. J'utilise, en passant, une barre latérale de navigation.

@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(); 
    } 

} 

Mise en page/content_main

En fait, ce que je faisais dans le content_main.xml est seulement des conteneurs pour les données remplaçables.

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

Initier le fragment

Ce que je l'ai fait sur le lancement du fragment est lorsqu'un utilisateur clique sur un élément dans la barre de navigation

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

Répondre

0

lors du chargement de votre fragment les ajouter à backstack

fragment = new CreateMessageFragment(); 
    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.replace(R.id.root_layout, fragment); 
    ft.addToBackStack(null) 
    ft.commit(); 

et dans votre activité presse Retour

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); 
       }