2015-10-19 470 views
0

想象一下3個片段的活動:開始顯示第一個片段,選擇一個菜單選項並轉到第二個片段,選擇另一個選項並轉到第三個片段並再次選擇第一個選項返回第二個片段一。片段堆棧Android

F1 - > F2 - > F3 - > F2

當我按回到我想要的應用程序返回到再次片段3,當我按回到它再次回到片段1,如果按,關閉應用程序。 就像片段存在一樣,將它移動到堆棧頂部,如果沒有,則創建它。

謝謝!

+0

使用urFragment.addToBackStack(空)的活動的

public abstract class FragmentsStackActivity extends BaseActivity { public static final String TAG_BUNDLE = "bundle_tag"; protected final Bundle fragmentArgs = new Bundle(); protected Stack<Fragment> fragments = new Stack<>(); abstract protected void setupFragments(); public void setFragmentArguments(Fragment fragment, Bundle arguments){ if(!fragments.isEmpty() && fragments.peek()!=fragment){ fragment.setArguments(arguments); } } public void setFragmentFromStack() { if(!fragments.isEmpty()) { Fragment fragment = fragments.peek(); final Fragment oldFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (oldFragment == null || oldFragment != fragment) { getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //transaction.setCustomAnimations(R.anim.animator_left_right_in, R.anim.animator_left_right_in); transaction.replace(R.id.fragment_container, fragment).commit(); } }else { finish(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //TODO need to save fragment stack } } 

例子;添加到backstack –

+0

是的,但是如果我選擇相同的選項兩次,它會增加兩次堆棧... – Yamila

+0

顯示一些代碼,您已嘗試完成此操作 –

回答

0

您可以用下面的代碼的幫助下實現這一點:

// transaction.replace(R.id.detailFragment, frag1); 
Transaction.remove(null).add(frag1) // frag1 on view 

// transaction.replace(R.id.detailFragment, frag2).addToBackStack(null); 
Transaction.remove(frag1).add(frag2).addToBackStack(null) // frag2 on view 

// transaction.replace(R.id.detailFragment, frag3); 
Transaction.remove(frag2).add(frag3) // frag3 on view 

而且爲了更好的理解,必須在下面的代碼片段一個OOK:

// Works with either the framework FragmentManager or the 
// support package FragmentManager (getSupportFragmentManager). 
getSupportFragmentManager().beginTransaction() 
         .add(detailFragment, "detail") 
         // Add this transaction to the back stack 
         .addToBackStack() 
         .commit(); 



getSupportFragmentManager().addOnBackStackChangedListener(
    new FragmentManager.OnBackStackChangedListener() { 
     public void onBackStackChanged() { 
      // Update your UI here. 
     } 
    }); 

看看這裏http://developer.android.com/training/implementing-navigation/temporal.html

+0

爲什麼要刪除片段1以添加片段2? – Yamila

+0

這樣,當你按下返回按鈕時,你將不會進入片段1 ...因爲它已經被片段2所取代...所以你可以做相關的東西來完成你的要求 – johnrao07

+0

我想返回片段1 ,我只想避免在堆棧中兩次使用相同的碎片... – Yamila

1

這是我一直想出來的解決方案。 這個想法如下,你需要保持一個堆棧數據結構,並且每當你添加一個片段時,也要將它添加到堆棧中,然後重寫onBackPress方法並檢查堆棧是否爲空,然後用新的片段替換你的片段容器當它是空的時候執行super.onbackpress 所以這裏是所有基於片段的導航的父類。擴展此類

public class LoginActivity extends FragmentsStackActivity{ 

    private final MyFragment1 fragment1 = new MyFragment1(); 
    private final MyFragment2 fragment2 = new MyFragment2(); 

    private final User mUser = new User(); 

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

     setupFragments(); 
     setFragmentFromStack(); 
    } 


    @Override 
    protected void setupFragments() { 
     fragments.add(fragment2); 
     //fragment2.setNotifier(this); // I use notifiers listener but you can choose whatever convenient for you 
     Bundle fragmentArgs = new Bundle(); 
     fragmentArgs.putBoolean(Constants.TAG_LOGIN, true); 
     fragmentArgs.putParcelable(User.TAG, mUser); 
     fragmentArgs.putInt(Constants.TYPE, getIntent().getIntExtra(Constants.TYPE, 0)); 
     fragment2.setArguments(fragmentArgs); 
     //fragment1.setNotifier(this); // I use notifiers listener but you can choose whatever convenient for you 
    } 


    // this method teals with handling messages from fragments in order to provide navigation 
    // when some actions taken inside the fragment, you can implement your own version 
    public void onReceiveMessage(String tag, Bundle bundle) { 
     switch (tag) { 
      case MyFragment2.TAG_BACK: 
      case MyFragment1.TAG_BACK: 
       fragments.pop(); 
       setFragmentFromStack(); 
       break; 
      case MyFragment2.TAG_NEXT: 
       fragment1.setArguments(bundle); 
       fragments.add(fragment1); 
       setFragmentFromStack(); 
       break; 
      case MyFragment1.TAG_NEXT: 
       goToWelcomeScreen(bundle); 
       finish(); 
       break; 
     } 
    } 

    private void goToWelcomeScreen(Bundle bundle){ 

    } 
}