Zhuinden / flowless

[DEPRECATED] Based on Flow 1.0-alpha. To keep your life simple, use zhuinden/simple-stack instead.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use Activity Context for inflating

theodm opened this issue · comments

Hey,

in my App i am working with the Vector Drawable support library. Internally it replaces normal ImageViews with it's AppCompat variant (android.support.v7.widget.AppCompatImageView).

The hooks which do this replacement are installed in the activity context. Yet (in the examples) the Dispatcher are given the Base context of the activity, which doesn't have these hooks on it.

override fun attachBaseContext(newBase: Context) {
    val baseContext = Flow.configure(newBase, this)
            .defaultKey(InsertDocumentKey())
            .dispatcher(flowDispatcher)
            .install()

    flowDispatcher.setBaseContext(baseContext)

    super.attachBaseContext(baseContext)
}

Using the Context in the Dispatcher looks like this:

val internalContext = traversal.createContext(newKey, baseContext)

val layoutInflater = LayoutInflater.from(internalContext)
val newView = layoutInflater.inflate(newKeyLayout, root, false)

Since we are using the base context, the replacement doesn't happen (which is the intended behaviour of the support library).

My proposal is, that the dispatcher should use the activity context instead of the base context of the activity.

I'm currently doing this to solve my problems:

flowDispatcher.setBaseContext(this) // this is an Activity

Are there any unintended behaviours i'm looking at with this approach?

Thank you very much for helping out :)

I think my oversight came from that the traversal.createContext() method claims to take baseContext as its parameter, but in reality this is just the base context of that specific wrapper, which can also be an Activity (which also has its own base context, the InternalContextWrapper).

So this means that using setBaseContext(this) would work and fix your problem.

I'll change the samples accordingly.

Hold on, it doesn't work well with ActivityUtils.getActivity() yet.

Fixing

@theodm nice find. I've used flowless in 3 projects and yet this hasn't come up to bite me yet, thanks for the report.

Fixed with 1.0-RC2 and a315f00

Thank you very much for that fast answer ;) Really appreciated!

Just a little thought:
Maybe you also want to rename the setBaseContext()/getBaseContext() methods of the dispatcher now, because it's not the base context of the containing activity anymore.

I'm not sure what to call it, because I basically laid that name down based on https://github.com/square/flow/blob/master/flow/src/main/java/flow/Traversal.java#L43 >.>

Also I was glad to finally get rid of the Activity constructor parameter.

I'll have to think about this, no guarantees about the name at this time.