Coder Social home page Coder Social logo

Comments (10)

JakeWharton avatar JakeWharton commented on August 27, 2024

No. View injection is not actually dependency injection it's just a convenience wrapper around calling findViewById and doing an implicit cast. A feature like this requires activities to extend from a common base class provided by the library which is a really bad thing to have to enforce. This is also further crippled by the fact that the constants in the R.java class are no longer final for library projects and thus cannot be used in annotations.

You could very easily write something to do this yourself since it doesn't actually require any libraries. Just create an annotation and in your common activity base class iterate over them and call findViewById. Another way which creates a cleaner looking onCreate method is just to have an implicit-casting version of findViewById like this:

/**
 * Finds a view that was identified by the id attribute from the XML that was processed in
 * {@link #onCreate(Bundle)} and performs an unchecked cast to the destination type.
 *
 * @param id View ID.
 * @param <T> Destination cast class type.
 * @return The view if found or {@code null} otherwise.
 */
@SuppressWarnings("unchecked")
public <T extends View> T findById(int id) {
  return (T) findViewById(id);
}

and then your onCreate becomes:

setContentView(R.layout.whatever);
TextView tv = findById(R.id.title);
SomeCustomView scv = findById(R.id.custom_view)

from dagger.

mecid avatar mecid commented on August 27, 2024

Thanks man!

from dagger.

staxgr avatar staxgr commented on August 27, 2024

I wrote the something exactly the same as the first suggestion from Jake a few months ago. It's just a couple of lines, feel free to copy it https://github.com/staxgr/ViewInject/blob/master/src/se/tap2/viewinject

from dagger.

JakeWharton avatar JakeWharton commented on August 27, 2024

@staxgr If you switch the annotation's properly to value it will be implicitly assigned and then you can do @InjectView(R.id.whatever).

from dagger.

christopherperry avatar christopherperry commented on August 27, 2024

@JakeWharton Wouldn't doing something like this violate one of the main reasons for using Dagger (avoiding the use of reflection in favor of compile time annotation processing)? I know casting the findViewById calls sucks, but your workaround is palatable in comparison.

from dagger.

JakeWharton avatar JakeWharton commented on August 27, 2024

Dagger is for dependency injection satisfied by code-gen and compile-time graph verification. View "injection" has nothing to do with dependencies and is merely made to emulate actual injection. I think myself and most would recommend the findById approach.

The @InjectView part above was just to demystify what it does. Someone could very easily create a tiny, standalone library that does code-gen for all @InjectView annotations and then satisfies them by calling Views.inject(this) on an activity or fragment.

from dagger.

christopherperry avatar christopherperry commented on August 27, 2024

@JakeWharton I understand that, I'm just pointing out that what staxgr wrote is not the approach one would want to take when using Dagger because it doesn't do code-gen, it uses reflection.

from dagger.

JakeWharton avatar JakeWharton commented on August 27, 2024

The @InjectView part above was just to demystify what it does. Someone could very easily create a tiny, standalone library that does code-gen for all @InjectView annotations and then satisfies them by calling Views.inject(this) on an activity or fragment.

I wrote this last night in a one-off experimentation with code generation for another project: http://github.com/JakeWharton/butterknife/

from dagger.

cgruber avatar cgruber commented on August 27, 2024

On 5 Mar 2013, at 13:38, Jake Wharton wrote:

I wrote this last night in a one-off experimentation with code
generation for another project:
http://github.com/JakeWharton/butterknife/

Heh. Butter-knife. Awesome.

from dagger.

christopherperry avatar christopherperry commented on August 27, 2024

@JakeWharton Domo arigato Sensei.

from dagger.

Related Issues (20)

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.