日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于fragment backstate的介绍

發布時間:2024/4/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于fragment backstate的介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Transaction BackStack and its management

Posted by?Achin?|?Filed under?Android 19.09.2014

This is second part of a 6 posts series. In the first post I talked around?basics of fragment oriented architecture. From this post onwards, I’ll be talking about it’s implementation details.

(Sample application’s?source code?and?README)

In this part I am going to talk about Transaction Backstack and few related methods that can be used frequently.
Transaction BackStack has often been misinterpreted as backstack of fragments. FragmentManager inside an activity deals with fragment-transactions rather than with fragments. An entry into this backstack is a ‘fragment-transaction’ which can be composed of one or more operations involving fragment(s). Reverting this would revert back all these operations together.

FragmentTransaction ft =?getFragmentManager().beginTransaction(); ft.add(restId, fragmentA); ft.replace(fragmentB); ft.commit();

Above, is a single transaction clubbing multiple operations.

Transactions do not get added to back stack by default. An explicit call toaddToBackStack(String tag)?before committing it, would add it to BackStack. The ‘tag’ string would be an identifier of the transaction and can be used to refer back to it later.

FragmentTransaction ft =?getFragmentManager().beginTransaction(); ft.add(resId, fragmentA); ft.replace(resId, fragmentB); ft.addToBackstack("tag"); ft.commit();

Reverse of this method is?popBackStack()?and its following variants.

  • popBackStack()?pops the top?transaction from stack.
  • popBackStack(String tag)?pops transactions until the transaction with supplied?‘tag’.
  • popBackStack(String tag,?int flag)?pops till the transaction with mentioned ‘tag’?if flag is POP_BACK_STACK_INCLUSIVE, or else?0?can be used.
  • popBackStack(int id,?int flag)?pops till the transaction with mentioned ‘tag’. Id is the identifier returned from FragmentTransaction.commit().

Above methods actually just enqueues a?transaction in FragmentManager’s ToDo queue and would come in effect only when?application returns to its event loop. In order to enforce the transactions to come in effect immediately, there are following additional variants with same effects respectively.

  • popBackStackImmediate()
  • popBackStackImmediate(String tag)
  • popBackStackImmediate(String tag, int flag)
  • popBackStackImmediate(int id, int flag)

How transaction backstack is not fragment backstack, is demonstrated in the sample app in ‘Back Stack Handler’ section as follows. If you press ‘Go to step 1’ button, the transaction?adds ?FirstStepFragment and commits. Thus, on?popBackStack()?from this fragment, app would remove FirstStepFragment and get back to ‘BackStackHandlerFragment’.

// TO ADD A FRAGMENT. @Override public void addFragment(BaseFragment baseFragment, boolean withAnimation) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction();if(withAnimation) { ft.setCustomAnimations(R.anim.fragment_slide_in_left, R.anim.fragment_slide_out_left, R.anim.fragment_slide_in_right, R.anim.fragment_slide_out_right); }ft.replace(R.id.home_frame_layout_for_fragments, baseFragment, baseFragment.getTagText());ft.addToBackStack(baseFragment.getTagText());ft.commit(); }

Instead, if you press ‘Go to step2’ in BackStackHandlerFragment, a transaction would occur with two ‘add’ operations, adding both FirstStepFragment and SecondStepFragment clubbed in a single transaction. So, a single?popBackStack()?call from SecondStepFragment would remove both these fragments and app would get back to BackStackHandlerFragment.

// TO ADD MULTIPLE FRAGMENTS CLUBBED INTO ONE TRANSACTION.@Override public void addMultipleFragments(BaseFragment[] baseFragments) {// Initialize a Fragment Transaction.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();// Record all steps for the transaction.for(int i = 0 ; i < baseFragments.length ; i++) {ft.setCustomAnimations(R.anim.fragment_slide_in_left, R.anim.fragment_slide_out_left, R.anim.fragment_slide_in_right, R.anim.fragment_slide_out_right);ft.replace(R.id.home_frame_layout_for_fragments, baseFragments[i], baseFragments[i].getTagText());}// Add the transaction to backStack with tag of first added fragmentft.addToBackStack(baseFragments[0].getTagText());// Commit the transaction.ft.commit(); }

Another?requirement could be of clearing the back stack.

// CLEAR BACK STACK. private void clearBackStack() {final FragmentManager fragmentManager = getSupportFragmentManager();while (fragmentManager.getBackStackEntryCount() != 0) {fragmentManager.popBackStackImmediate();} }

The above method loops over all the transactions in the backstack and removes them immediately?one at a time.

You may?want to take?a look at the following as well while dealing with transaction backstack management.

  • OnBackStackChangedListener
  • FragmentManager.getbackStackEntry(int index)

總結

以上是生活随笔為你收集整理的关于fragment backstate的介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。