Comments (10)
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.
Thanks man!
from dagger.
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.
@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.
@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.
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.
@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.
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 callingViews.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.
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.
@JakeWharton Domo arigato Sensei.
from dagger.
Related Issues (20)
- java.lang.NoClassDefFoundError: dagger.internal.Preconditions HOT 2
- java.lang.NoClassDefFoundError: dagger.internal.Preconditions HOT 1
- android.app.Application does not implement dagger.android.HasActivityInjector HOT 3
- --
- Android Context HOT 1
- Subcomponent inside subcomponent HOT 1
- Error when a component is trying to include bindings with different scopes could be more informative HOT 1
- Constructor injection cannot find symbol method inject members dagger2 2.14 HOT 1
- how can i run the example of CoffeeApp.java HOT 1
- Why do Singletons need an empty default constructor? HOT 2
- dagger-compiler
- dagger.android.DispatchingAndroidInjector cannot be provided without an @Provides HOT 1
- 每次修改完java代码都需要clean项目 才能运行, HOT 1
- did u consider this kind of case: ChildFragment needs to reuse ParentFragment's object instead of creating a new one。hilt may not support this kind of case
- duplicate Nullable HOT 2
- cannot generate
- Support extending base builder classes HOT 2
- The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found. HOT 1
- Issue with enableAggregatingTask flag / AGP 7.0 / oss-licenses HOT 1
- after add hilt { enableAggregatingTask = true } dependencies tasks faild
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dagger.