2017-11-18 7 views
0

に行く私は、このファイルに以下のようなactivity_settings.xmlファイルを持って戻って押した場合、それはフラグメント私は私は1つをクリックした場合、私は3つのテキストの景色を眺めることができ、ボタンをSettingsActivity.class

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.crowderia.chat.SettingsActivity" 
    android:background="@color/black"> 

    <TextView 
     android:id="@+id/tv_added_me" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Added Me" /> 

    <TextView 
     android:id="@+id/tv_add_friends" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add Friends" /> 

    <TextView 
     android:id="@+id/tv_my_friends" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="My Friends" /> 

    <FrameLayout 
     android:id="@+id/settings_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </FrameLayout> 

</RelativeLayout> 

私に私を取るに各テキストビューの

のonclickイベント以下のようなSettingsActivity.class

tv_added_me.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       setFragment(new AddedMeFragment()); 
      } 
     }); 

セットフラグメント方法

public void setFragment(Fragment f) { 

     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); 
     fragmentTransaction.replace(R.id.settings_fragment, f); 
     fragmentTransaction.commit(); 
    } 

私は、各テキストビューをクリックした場合、それは、各フラグメントに私を取るだろうが、私はその設定]活動に行くつもりはないバックをクリックした後は、どのように私はこれらの新しい私にイムを助けてください。この作品に

を作ることができますstuff

答えて

1

戻るボタンをクリックしてFragmentManagerからすべてのフラグメントを削除する必要があります。

FragmentManager fm = getSupportFragmentManager(); 
for (Fragment f : fm.getFragments()) { 
    fm.beginTransaction().remove(f).commit(); 
} 
+0

ここでは、私は少し説明することができますか?@override onBackPressed()メソッドで – HemalHerath

+1

をしてください。 –

+0

デバイスバックボタンを実行する場合は、onBackPressed()に書き込む必要があります。あなたが戻る矢印付きのツールバーを使用している場合は、戻る矢印のクリックで記述する必要があります。 – Wizard

関連する問題