2015-10-24 89 views
1

我有活動,有兩個片段。 FragmentSurahDetailFragmentTafsir。當事件「OnOpenTafsir」時添加FragmentTafsir。如果添加FragmentTafsir並更改方向,則FragmentTafsir將從屏幕消失。任何想法如何解決它?片段從屏幕上消失

這裏是我的代碼:

public class ActivitySurahDetail extends AppCompatActivity implements FragmentSurahDetail.SurahListener { 
    Toolbar toolbar; 
    final String LOG_TAG = "ASD Tag"; 
    private static final String TAFSIR_FRAGMENT_TAG = "tafsir_fragment"; 
    private static final String SURAH_DETAIL_TAG = "surah_fragment"; 
    int surah_id; 
    FragmentTafsir fragmentTafsir; 
    private boolean favoriteMode; 
    FragmentTransaction fragmentTransaction; 
    FragmentSurahDetail fragmentSurahDetail; 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       super.onBackPressed(); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.surah_detail_activity); 
     Bundle extras = getIntent().getExtras(); 
     surah_id = extras.getInt("item_id"); 
     favoriteMode = extras.getBoolean("favoriteMode", false); 
     fragmentSurahDetail = new FragmentSurahDetail(); 
     Bundle args = new Bundle(); 
     args.putBoolean("favoriteMode", true); 
     args.putInt("item_id", surah_id); 
     fragmentSurahDetail.setArguments(args); 
     getSupportFragmentManager().beginTransaction().replace(R.id.surah_detail_container, fragmentSurahDetail, SURAH_DETAIL_TAG).commit(); 
     // Show the Up button in the action bar. 
     toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    } 


    @Override 
    public void onOpenTafsir(int surah, int ayah) { 
     fragmentTafsir = new FragmentTafsir(); 
     fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     Bundle extras = new Bundle(); 
     extras.putInt("surah", surah); 
     extras.putInt("ayah", ayah); 
     fragmentTafsir.setArguments(extras); 
     fragmentTransaction.addToBackStack(TAFSIR_FRAGMENT_TAG); 
     fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
     fragmentTransaction.replace(R.id.surah_detail_container, fragmentTafsir, TAFSIR_FRAGMENT_TAG).commit(); 
    } 
} 

回答

0

解決。我剛剛添加了該行

if (getSupportFragmentManager().findFragmentByTag(SURAH_DETAIL_TAG) == null){ 
      getSupportFragmentManager().beginTransaction().replace(R.id.surah_detail_container, fragmentSurahDetail, SURAH_DETAIL_TAG).commit(); 
     }