All Projects → evernote → Android State

evernote / Android State

Licence: epl-1.0
A utility library for Android to save objects in a Bundle without any boilerplate.

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Android State

Bulldog
Android library to simplify reading and writing to SharedPreferences, never write code like this anymore prefs.edit().putString("someKey","someString").apply()
Stars: ✭ 133 (-84.48%)
Mutual labels:  annotation-processor, boilerplate
Android Clean Architecture Boilerplate
An android boilerplate project using clean architecture
Stars: ✭ 3,534 (+312.37%)
Mutual labels:  boilerplate, android-application
Parceler
📦 Android Parcelables made easy through code generation.
Stars: ✭ 3,589 (+318.79%)
Mutual labels:  annotation-processor, boilerplate
Preact Redux Typescript Rollup Starter
Smallest "React-like" + Redux starter EVER - 13KB min&gzip
Stars: ✭ 24 (-97.2%)
Mutual labels:  boilerplate
Androidpn Client
Android Push Notification cilent
Stars: ✭ 25 (-97.08%)
Mutual labels:  android-application
Snowball
Android Clean Code Sample Project
Stars: ✭ 26 (-96.97%)
Mutual labels:  android-application
3ree
An example universal JS application written with the 3REE stack, React + Redux + RethinkDB + Express. A stack for building apps, front and back end, with just Javascript.
Stars: ✭ 856 (-0.12%)
Mutual labels:  boilerplate
Preact Jest Snapshot Test Boilerplate
🚀 Test Preact components using Jest snapshots
Stars: ✭ 24 (-97.2%)
Mutual labels:  boilerplate
Vue Enterprise Boilerplate
An ever-evolving, very opinionated architecture and dev environment for new Vue SPA projects using Vue CLI.
Stars: ✭ 7,354 (+758.11%)
Mutual labels:  boilerplate
Vue Stack
Minimalistic Boilerplate for FullStack Express and Vue.js applications
Stars: ✭ 26 (-96.97%)
Mutual labels:  boilerplate
Angular Go Boilerplate
A simple boilerplate project with Angular 4 on Front-End and Golang on Backend
Stars: ✭ 26 (-96.97%)
Mutual labels:  boilerplate
Svelte Tailwind Extension Boilerplate
A Chrome extension boilerplate built with Svelte, TailwindCSS, Jest, and Rollup.
Stars: ✭ 26 (-96.97%)
Mutual labels:  boilerplate
Next Boilerplate
A well-structured production ready Next.js boilerplate with Typescript, Redux, Jest, Enzyme, Express.js, Sass, Css, EnvConfig, Fetch, Reverse Proxy, Bundle Analyzer and Built-in Project CLI. https://pankod.github.io/next-boilerplate/
Stars: ✭ 936 (+9.22%)
Mutual labels:  boilerplate
Youtubedownloader
Android App for searching for videos on Youtube by keywords using YouTube Data API and download videos from YouTube in different formats.
Stars: ✭ 25 (-97.08%)
Mutual labels:  android-application
Fusell Seed
FUSE (the low-level interface) file system boilerplate 📂 🔌 💾
Stars: ✭ 9 (-98.95%)
Mutual labels:  boilerplate
Dbweather
Android weather application with news feed and live tv
Stars: ✭ 24 (-97.2%)
Mutual labels:  android-application
Meteor Boilerplate
A lightweight boilerplate for meteor projects
Stars: ✭ 841 (-1.87%)
Mutual labels:  boilerplate
Kodein Mvvm
Example app using Kodein for dependency injection with MVVM and Architecture Components
Stars: ✭ 26 (-96.97%)
Mutual labels:  android-application
Laravel6 Frontend Boilerplate
A Vue.js Frontend starter project kit template/boilerplate with Laravel 6 Backend API support.
Stars: ✭ 26 (-96.97%)
Mutual labels:  boilerplate
Insights.py
AppetizerIO CLI for Mobile DevOps Development
Stars: ✭ 26 (-96.97%)
Mutual labels:  android-application

DEPRECATED

This library is not maintained anymore.

Android-State

A utility library for Android to save objects in a Bundle without any boilerplate. It uses an annotation processor to wire up all dependencies.

Download

Download the latest library and processor or grab via Gradle:

dependencies {
    implementation 'com.evernote:android-state:1.4.1'
    // Java only project
    annotationProcessor 'com.evernote:android-state-processor:1.4.1'

    // Kotlin with or without Java
    kapt 'com.evernote:android-state-processor:1.4.1'
}

You can read the JavaDoc here.

Usage

It's recommended to turn on a global setting in your Application class to save and restore the instance state of all activities and fragments from the support library. After that you only need to annotate your fields with @State inside of your fragments and activities. You can save any type which can be saved in a Bundle like the String, Serializable, and Parcelable data structures.

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        StateSaver.setEnabledForAllActivitiesAndSupportFragments(this, true);
    }
}

public class MainActivity extends Activity {

    @State
    public int mValue;

    // ...
}

class MainFragment : Fragment() {

    @State
    var title = "My fragment"
}

If you want to save the state of any other object or didn't turn on the global setting, then you need to use the StateSaver class for manually saving and restoring the instance state.

public class MainActivity extends Activity {

    @State
    public int mValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StateSaver.restoreInstanceState(this, savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        StateSaver.saveInstanceState(this, outState);
    }
}

Advanced

You can also save state in a View class.

public class TestView extends View {

    @State
    public int mState;

    public TestView(Context context) {
        super(context);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        return StateSaver.saveInstanceState(this, super.onSaveInstanceState());
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        super.onRestoreInstanceState(StateSaver.restoreInstanceState(this, state));
    }
}

It is recommended that saved properties not be private. If a property is private, then a non-private getter and setter method are required. This is especially useful for Kotlin, because properties are private by default and the aforementioned methods are generated by the compiler.

If you want your getter and setter to be used rather than the field value being used directly, the field must be private.

class DemoPresenter : Presenter<DemoView>() {

    @State
    var counter = 0

    // ...
}

Of course, this also works in Java.

public class TitleUpdater {

    @State
    private String mTitle;

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }
}

If you have a private field and don't want to provide a getter or setter method, then you can fallback to reflection. However, this method is not recommended.

public class ImageProcessor {

    @StateReflection
    private byte[] mImageData;

    // ...
}

A custom bundler can be useful, if a class doesn't implement the Parcelable or Serializable interface, which oftentimes happens with third party dependencies.

public class MappingProvider {

    @State(PairBundler.class)
    public Pair<String, Integer> mMapping;

    public static final class PairBundler implements Bundler<Pair<String, Integer>> {
        @Override
        public void put(@NonNull String key, @NonNull Pair<String, Integer> value, @NonNull Bundle bundle) {
            bundle.putString(key + "first", value.first);
            bundle.putInt(key + "second", value.second);
        }

        @Nullable
        @Override
        public Pair<String, Integer> get(@NonNull String key, @NonNull Bundle bundle) {
            if (bundle.containsKey(key + "first")) {
                return new Pair<>(bundle.getString(key + "first"), bundle.getInt(key + "second"));
            } else {
                return null;
            }
        }
    }
}

Lint

The library comes with Lint rules to verify a correct usage of the library. The lint checks work in Java and Kotlin files.

ProGuard

This library comes with a ProGuard config. No further steps are required, but all necessary rules can be found here.

Icepick

This library is based on Icepick, a great library from Frankie Sardo. However, Icepick is missing some features important to us: it doesn't support properties which is a bummer for Kotlin. Also, Icepick does not support private fields which may break encapsulation. A tool shouldn't force you into this direction.

Since Icepick is implemented in Clojure, we decided that it's better for us to rewrite the annotation processor in Java. Unfortunately, that makes it hard to push our features into Icepick itself. That's why we decided to fork the project.

There are also alternatives for Kotlin like IceKick. We did not want to use two libraries to solve the same problem for two different languages; we wanted to have one solution for all scenarios.

Upgrading to this library from Icepick is easy. The API is the same; only the packages and the class name (i.e. from Icepick to StateSaver) have changed. If Icepick works well for you, then there's no need to upgrade.

License

Copyright (c) 2017 Evernote Corporation.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at

http://www.eclipse.org/legal/epl-v10.html

Files produced by Android-State code generator are not subject to terms
of the Eclipse Public License 1.0 and can be used as set out in the
copyright notice included in the generated files.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].