Fragment solutions for me: fragmentation

Source: Internet
Author: User

Fragment Series Articles:
1, Fragment full analytical series (a): Those who have stepped on the pit
2, Fragment full analytical series (ii): Correct use of posture
3, fragment of my solution: fragmentation

If you read through the first two of the series, I'm sure you can write the fragment that most of the scenes will work. If you want to know more, then you can look at the library I encapsulated: fragmentation.
This article mainly introduces this library, solves some bugs, the use is simple, provides the real-time view stack view and so on practical function.

Source Address: Github, welcome fork, mention issues.

Demo Network disk download
Demo Demo: Single activity+ multi-Fragment


Demo.giffragmentation

For "single activity + multi-Fragment Architecture", "multi-module activity + multi-Fragment architecture", to help you simplify the use of the process, fixed some of the official fragment library bug.

Characteristics

1, the use of fragment to the degree of health
2, provide a convenient way to manage the fragment
3, effectively solve fragment overlap problem
4, Real-time view of fragment (including nested fragment) stack view, easy to fragment when nested debugging
5, increase the startup mode, Startforresult and other similar activity methods
6. fix some bugs when pop (TAG/ID) in the official library pops up multiple fragment
7, the perfect solution in and out of the stack animation of some bugs, more free management fragment animation

How to use

1, the project under the build.gradle of the app depends on:

‘me.yokeyword:fragmentation:0.3.0‘// appcompat v7包也是必须的

2, the activity inherits Supportactivity:

PublicClassMainactivityExtendssupportactivity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (...);if (savedinstancestate = =NULL) {Start (homefragment.newinstance ()); }} /** * Set container ID, must implement */ @Override public int Setcontainerid () {return r.id.fl_container;} /** * Set global animation, in supportfragment can freely change its animation */ @Override protected fragmentanimator Oncreatefragmentanimator () { //default vertical (same as Android 5.0 + animation) return  Super.oncreatefragmentanimator (); //set Landscape (same as Android 4.x animation) //return new Defaulthorizontalanimator () ; //set Custom Animation //return new Fragmentanimator (Enter,exit,popenter , popexit); }

3, fragment Inherit Supportfragment

Api

Supportactivity
Opens the stack view prompt box, in the complex nesting time, can through this to clean up the different class the stack view.

// 弹出 栈视图 提示框showFragmentStackHierarchyView();

In addition to the most supportfragment methods, please check them yourself.

Supportfragment
1. Start-Up Related:

// 启动新的Fragmentstart(SupportFragment fragment)// 以某种启动模式,启动新的Fragmentstart(SupportFragment fragment, int launchMode)// 启动新的Fragment,并能接收到新Fragment的数据返回startForResult(SupportFragment fragment,int requestCode)// 启动目标Fragment,并关闭当前FragmentstartWithFinish(SupportFragment fragment)

2. Close fragment:

// 关闭当前Fragmentpop();// 关闭某一个Fragment栈内之上的FragmentspopTo(Class fragmentClass, boolean includeSelf);// 如果想出栈后,紧接着开始.beginTransaction()开始一个事务,请使用下面的方法:// 在 第二篇 文章内的 Fragment事务部分的问题 有解释原因popTo(Class fragmentClass, boolean includeSelf, Runnable afterTransaction)

3, in Supportfragment, support monitoring return button event:

@Overridepublic boolean onBackPressedSupport() { // 返回false,则继续传递返回事件; 返回true则不继续传递 return false;}

4, define the current fragment animation, the onCreateFragmentAnimation method of replication:

@Overrideprotected FragmentAnimator onCreateFragmentAnimation() {    // 获取在SupportActivity里设置的全局动画对象 FragmentAnimator fragmentAnimator = _mActivity.getFragmentAnimator(); fragmentAnimator.setEnter(0); fragmentAnimator.setExit(0); return fragmentAnimator; // 也可以直接通过 // return new FragmentAnimator(enter,exit,popEnter,popExit)设置一个全新的动画}

5, get the current activity/fragment inside the stack Top (sub) Fragment:

getTopFragment();

6. Get a Fragment object in the stack:

findFragment(Class fragmentClass);// 获取某个子Fragment对象findChildFragment(Class fragmentClass);

More
Hide/Show Input Method:

// 隐藏软键盘 一般用在onHiden里hideSoftInput();// 显示软键盘showSoftInput(View view);

Here is startForResult the code for Detailfragment modifytitlefragment:

detailfragment.class: Startforresult ( Modifydetailfragment.newinstance (Mtitle), req_code);  @Override public void onfragmentresult ( int Requestcode, int ResultCode, Bundle data) {super.onfragmentresult (Requestcode, ResultCode, data); if (Requestcode = req_code && ResultCode = = RESULT_OK) { //here to get the returned data via bundle data}} Modifytitlefragment.class: Bundle bundle = new Bundle (); Bundle.putstring (" title ", " xxxx "); Setframgentresult (RESULT_OK, bundle);     

Below is the standard code for a fragment in a singletask mode start :

homefragment fragment = Findfragment (Homefragment.class); if (fragment = = null) {fragment = Homefragment.newinstance ();}    else{bundle Newbundle = new bundle (); //the bundle data passed, the fragment (bundle Onnewbundle) method of the target newbundle is called Fragment.putnewbundle ( Newbundle);} //homefragment start start in Singletask mode (fragment, supportfragment.singletask); //in Homefragment.class:  @Override protected void onNewBundle//can receive data here}      
About fragmentation to help you get back to fragment, you need to know.

2 Concepts:

"Sibling" type: such as the main interface of QQ, "message", "Contact", "dynamic", these 3 fragment belong to sibling relationship
"Process" type: for example, Login--Registration/Forgot password--fill in the information, jump to the homepage activity

For the "process" type fragments in activity (such as login, register/forgot password and fill in the information and jump to the home activity), fragmentation helps you handle the recovery within the stack, Make sure the fragment doesn't overlap and you don't have to deal with it yourself.

However, if the fragments in your activity are "siblings", then you need to onHandleSaveInstanceState() use replication findFragmentByTag(tag) or getFragments() go back to the process.

@Override    protected void onHandleSaveInstancState(Bundle savedInstanceState) { // 复写的时候 下面的super一定要删掉 // super.onHandleSaveInstancState(savedInstanceState); // 在此处 通过findFragmentByTag或getFraments来恢复,详情参考第二篇文章 }

And if you have fragment nested, then whether it is "sibling" or "flow" type, you need to restore processing yourself.



Wen/yokeyword (author of Jane's book)
Original link: http://www.jianshu.com/p/38f7994faa6b
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Fragment solutions for me: fragmentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.