Git Product home page Git Product logo

android_global_context's Introduction

android_global_context

What

Easy way to get application context in your Android project

Why

Context is very commonly used parameter in Android development.

We use context for app info, resources, system services, user configurations.

There are two ways to get context:

  • Add context as parameter

when we want context in low level library to get resources, we pass context as parameter and pass it one by one, that's too boring.

If you forget pass context from beginning, it's a lot work to do.

Adding a context parameter change the old method signature, this may cause a series of compile error.

  • Use Application Singleton

You can simple modify your Application instance become a singleton, so that can be accessed in anywhere.

However, if your project is multiple modules, application singleton is often in App module, which is the top of your multiple modules hierarchy, other low level modules can not depend App module, thus they can not access application singleton anymore.

Thank goodness, the Android Team finally discovery this problem, they developed a androidx library to provider global context, which use a singleton to hold application instance when ContentProvider attach method called.

This work is smart, but still involved a additional ContentProvider component.

We want a simple and light way to get context anywhere

How

We use the hidden class ActivityThread

    ActivityThread:
    
    public static Application currentApplication() {
        ActivityThread am = currentActivityThread();
        return am != null ? am.mInitialApplication : null;
    }
    

https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/ActivityThread.java?q=activitythread&ss=android

As you can see, ActivityThread class has method currentApplication from Android 4.x to Android 13,

and it's a public method, used internally by the Android Team, so it's safe to use by us.

Cause the Android Team removed ActivityThread.class when packing Android.jar, so we need a little trick to do.

Good news is the tricky thing has been done for you, you just need add the dependency and joy it!

If you are interested in a little trick , click this

code:

    AppGlobal.getApplication()
    AppGlobal.getPackageName()
    AppGlobal.getProcessName()

gradle:

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

// add dependency
dependencies { 
        implementation "com.github.fatterx:android_global_context:0.0.1"
}

relative links:

android_global_context's People

Contributors

fatterx avatar

Stargazers

 avatar  avatar Scott Beeker avatar

Watchers

 avatar

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.