Coder Social home page Coder Social logo

nakamuuu / retainer Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 2.0 229 KB

An Android library that helps you retain objects in an Activity / Fragment during configuration changes.

License: Apache License 2.0

Java 100.00%
android java android-library

retainer's Introduction

Retainer

Release CircleCI

Retainer is an Android library that helps you retain objects in an Activity / Fragment during configuration changes.

It auto-generates container objects by using annotation processing, and this objects will be internally preserved in a Fragment. setRetainInstance(true) is called for this fragment, so that stateful objects are preserved during configuration changes.

If you need more information about the handling of configuration changes, check the following document.

https://developer.android.com/guide/topics/resources/runtime-changes.html

NOTE: When your activity or application process have been destroyed, Fragment in which setRetainInstance(true) is called is also destroyed. Therefore, this approach doesn't replace the method using onSaveInstanceState().

Lifetime of objects

How to use

Retainer also provides you static methods that bundle a procedure related to Fragment's lifecycle. So you can simply retain objects by following the steps and examples below.

  1. Add @Retain to a field you want to retain.
  2. Call Retainer.bind(this) in your activity or fragment.

IMPORTANT: You should never add @Retain to an object that is tied to the activity context, such as a Drawable, an Adapter, or a View. If you do, it will cause serious memory leaks.

Activity

Supports only androidx.fragment.app.FragmentActivity or its subclasses.

import net.divlight.retainer.Retainer;
import net.divlight.retainer.annotation.Retain;

public class ExampleActivity extends AppCompatActivity {
    // This field will be automatically preserved by Retainer.
    @Retain
    int retainedValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Retainer.bind(this);
    }
}

Fragment

Supports only androidx.fragment.app.Fragment or its subclasses.

import net.divlight.retainer.Retainer;
import net.divlight.retainer.annotation.Retain;

public class ExampleFragment extends Fragment {
    // This field will be automatically preserved by Retainer.
    @Retain
    int retainedValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Retainer.bind(this);
    }
}

Download

Gradle

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency in your module's build.gradle:

implementation "com.github.nakamuuu.retainer:library:${latest_version}"
annotationProcessor "com.github.nakamuuu.retainer:processor:${latest_version}"

Requirements

Android 4.0.3+ (API Level: 15+)

License

Copyright 2017 Keita Nakamura

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

retainer's People

Contributors

nakamuuu avatar hkurokawa avatar

Stargazers

Pranav Lathigara avatar Ryoga Kitagawa avatar yoshimaa avatar Haruki Hasegawa avatar marilla716 avatar Tomoya Miwa avatar masaibar avatar Issei Aoki avatar Tatsuya Arai avatar JitPack.io avatar  avatar

Watchers

Tatsuya Arai avatar  avatar

Forkers

hkurokawa daxeshv

retainer's Issues

IllegalStateException occurs when used in Fragment

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)
       at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1860)
       at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:649)
       at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609)
       at net.divlight.retainer.Retainer.onCreate(Retainer.java:31)

It should be modified to use FragmentTransaction#commitAllowingStateLoss() instead of commit().
RetainerFragment has no fields saved by using onSavedInstanceState() . So It seems that this change won't cause any side effects ๐Ÿค”

The generated object's name may be improper

Example

public class TestActivity {
   public class InnerClass1 {
        public class InnerClass2 {
            @Retain int value;
        }
   }
}

The auto-generated object name is expected to TestActivity$InnerClass1$InnerClass2_Retainer , but the actual is InnerClass1$TestActivity$InnerClass2_Retainer ๐Ÿ˜ข

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.