2014-06-17 108 views
0

我使用android支持庫,我有3個片段的活動。 ,我有一個問題:替換片段的刪除其他片段Android:替換片段刪除其他片段

我的活動:

public class MainActivity extends FragmentActivity { 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Add 3 fragments: 

     if (savedInstanceState == null) { 

      getFragmentManager().beginTransaction() 
        .add(R.id.container1, new A(), A.class.getCanonicalName()) 
        .commit(); 
      getFragmentManager().beginTransaction() 
        .add(R.id.container2, new B(), B.class.getCanonicalName()) 
        .commit(); 
      getFragmentManager().beginTransaction() 
        .add(R.id.container3, new C(), C.class.getCanonicalName()) 
        .commit(); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 
     if (id == R.id.test) { 

      Fragment fragment = getFragmentManager(). 
        findFragmentByTag(A.class.getCanonicalName()); 
      if (fragment == null) 
       fragment = new A(); 

      // Replace fragment A again 
       getFragmentManager() 
         .beginTransaction() 
         .replace(R.id.container1, fragment, fragment.getClass().getCanonicalName()) 
         .commit(); 
      // After this fragment B disappears 


      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

我使用findFragmentByTag避免內存泄漏。 你能幫我嗎?

+0

嘗試增加這片段的交易,之前提交 '//添加本次交易後堆 .addToBackStack()' –

+0

不知道你的問題是清楚的,因爲你做.replace這樣按預期的方式取代了以往的片段。現在@IonutNegru說,如果問題是你不能倒退,那麼使用addToBackStack,但問題並不清楚。 – HpTerm

+0

@HpTerm也是對的,你應該使用add來避免替換當前片段。我錯過了那一個。 您可以查看碎片的官方文檔。你的情況也有一些例子。 –

回答

0

試試這個方法:

做出共同馬託在您的容器中添加片段,只是把這個代碼,你的方法,並通過您的片段在mehod,如:

public void _LoadFragment(Fragment _frag){ 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.container, _frag); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
} 
1
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

// Replace whatever is in the fragment_container view with this fragment, 
// and add the transaction to the back stack so the user can navigate back 
transaction.replace(R.id.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit(); 

試試這個代碼來替換任何片段

0
public class MainActivity extends FragmentActivity { 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Add 3 fragments: 

     if (savedInstanceState == null) { 

      getFragmentManager().beginTransaction() 
        .add(R.id.container1, new A(), A.class.getCanonicalName()) 
        .commit(); 
      getFragmentManager().beginTransaction() 
        .add(R.id.container2, new B(), B.class.getCanonicalName()) 
        .commit(); 
      getFragmentManager().beginTransaction() 
        .add(R.id.container3, new C(), C.class.getCanonicalName()) 
        .commit(); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 
     if (id == R.id.test) { 

      Fragment fragment = getFragmentManager(). 
        findFragmentByTag(A.class.getCanonicalName()); 
      if (fragment == null) 
       fragment = new A(); 

      // Replace fragment A again 

      // After this fragment B disappears 
FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
      fragmentTransaction.replace(R.id.container1, fragment); 
fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 

      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

試試這個