Skip to content

zcwk/StatedFragment

 
 

Repository files navigation

Android Arsenal

StatedFragment

关于这个库的详细说明在这里: Android中保存和恢复Fragment状态的最好方法 继承StatedFragment,同时分别在onSaveState(Bundle outState)和onRestoreState(Bundle savedInstanceState)中保存和取出状态数据。如果你想在fragment第一次启动的时候做点什么,你也可以重写onFirstTimeLaunched(),它只会在第一次启动的时候被调用。

Version

0.10.0

Installation

To use this library in your android project, just simply add the following dependency into your build.gradle

StatedFragment for Android Support Library v4's Fragment

dependencies {
    compile 'com.inthecheesefactory.thecheeselibrary:stated-fragment-support-v4:0.10.0'
}

StatedFragment for Android's Fragment

dependencies {
    compile 'com.inthecheesefactory.thecheeselibrary:stated-fragment:0.10.0'
}

Usage

onActivityResult for Nested Fragment

In v0.10.0 onward, NestedActivityResultFragment is introduced to fix onActivityResult problem which couldn't be called on nested fragment. To use it, you have to override onActivityResult on your Activity and add a line of code:

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ActivityResultBus.getInstance().postQueue(new ActivityResultEvent(requestCode, resultCode, data));
    }

And in your fragment, you need to call getActivity().startActivityForResult(...) but not startActivityForResult(...) since we need to let all the result sent to Activity.

Lastly, override onActivityResult in your fragment in the standard way.

    public class MainFragment extends NestedActivityResultFragment {
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            // Add your code here
            Toast.makeText(getActivity(), "Fragment Got it: " + requestCode + ", " + resultCode, Toast.LENGTH_SHORT).show();
        }
        
    }

State Saving/Restoring

Since v0.10.0, StatedFragment is now marked deprecated. Please use the traditional way documented at http://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en instead

Change Logs

v0.10.0

  • StatedFragment is marked deprecated. Traditional way to save/restore fragment's state is recommended.
  • NestedActivityResultFragment is introduced to fix onActivityResult problem which couldn't be called on nested fragment

v0.9.3

Add support to <fragment> tag.

v0.9.2

Add onActivityResult feature for nested fragment.

v0.9.1

Change Android Support Library v4 dependency's version to 21.+ to avoid future dependency conflict.

v0.9.0

Seperates library into two versions:

  • stated-fragment-support-v4: Fragment is inherited from android.support.v4.app.fragment
  • stated-fragment: Fragment is inherited from android.app.fragment

License

Apache 2.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%