Hi,
I want to Add ButterKnife.inject(this); in BaseActivity like this.
public class BaseActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.inject(this);
}
}
and use it in child activity like
public class TestActivity extends BaseActivity {
@InjectView(R.id.lv_bom)
ListView lvTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bomconfiguration_list);
lvTest.setAdapter(adpter);
}
}
How can I use ButterKnife in this case.
I don't want add ButterKnife.inject(this); in all activity.
This works. if it doesn't work, file a specific bug.
I think author of the issue means the problem of calling ButterKnife
inject() in super.onCreate(), which will be called before setContentView()
in child class.
29 мая 2015 г. 12:18 PM пользователь "Jake Wharton" <
[email protected]> написал:
This works. if it doesn't work, file a specific bug.
—
Reply to this email directly or view it on GitHub
https://github.com/JakeWharton/butterknife/issues/268#issuecomment-106753508
.
Gotcha. Butter Knife is not magical and is actually a very boring, stupid, and predicable library. If you call Butter Knife before setContentView you'll get 0 views.
To the author of the issue: if you really want to call ButterKnife#inject()
only from Base class, take a look at Activity#onPostCreate() or override
Activity#setContentView(), call super.setContentView() and then
ButterKnife#inject().
For Fragments you can use Fragment#onViewCreated().
Personally, I don't think that view binding in Base class is good idea.
29 мая 2015 г. 12:27 PM пользователь "Jake Wharton" <
[email protected]> написал:
Gotcha. Butter Knife is not magical and is actually a very boring, stupid,
and predicable library. If you call Butter Knife before setContentView
you'll get 0 views.—
Reply to this email directly or view it on GitHub
https://github.com/JakeWharton/butterknife/issues/268#issuecomment-106756557
.
The library was designed to be called in a base class and will no-op if the
implementing class has no injections.
On Fri, May 29, 2015 at 2:39 AM Artem Zinnatullin [email protected]
wrote:
To the author of the issue: if you really want to call ButterKnife#inject()
only from Base class, take a look at Activity#onPostCreate() or override
Activity#setContentView(), call super.setContentView() and then
ButterKnife#inject().For Fragments you can use Fragment#onViewCreated().
Personally, I don't think that view binding in Base class is good idea.
29 мая 2015 г. 12:27 PM пользователь "Jake Wharton" <
[email protected]> написал:Gotcha. Butter Knife is not magical and is actually a very boring,
stupid,
and predicable library. If you call Butter Knife before setContentView
you'll get 0 views.—
Reply to this email directly or view it on GitHub
<
https://github.com/JakeWharton/butterknife/issues/268#issuecomment-106756557.
—
Reply to this email directly or view it on GitHub
https://github.com/JakeWharton/butterknife/issues/268#issuecomment-106759165
.
Got it. How can use as I describe above.
Thanks for your help.
Thank you.
overriding setContentView(int) and setContentView(View) then call inject works fine.
(might needs some care if you call setContentView multiple times)
Is there any problem if i inject it on BaseActivity like
ButterKnife.bind(BaseActivity.this);
and inject in ChildActivity which extends BaseActivity too like
ButterKnife.bind(ChildActivity.this);
? Or just injecting in Base is enough?
That will do the same injection twice since both of those this's refer to the same instance.
Hey there! I know this is really old but I just wanted to leave this here for anyone who gets here through Google like I did. I had the same issue. For my scenario, in my BaseActivity class, I used the onContentChanged() function to bind the activity to the current content. For my BaseFragment class, I used the onViewCreated() function to bind my view so that I never have to bind in my sub classes and I can use binding without worrying about anything. Peace :v:
Hi,
I implement "ButterKnife.bind(this, view!!)" in "onViewStateRestored" method in BaseFragment. But extended Fragment buttons are still null. Why?
Found a Solution for binding views in child Activities from BaseActivity
put this custom setContentView in BaseActivity
```
public void customSetContentView(int res_id, AppCompatActivity activity)
{
super.setContentView(res_id);
ButterKnife.bind(activity);
}
And now call this `customSetContentView` instead of default ` setContentView` method from any BaseActivity child, the only difference is that you have to pass child activity as a parameter BaseActivity customSetContetnView method.
@BindView(R.id.tv_test)
TextView tv_test;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
customSetContentView(R.layout.activity_main, this);
tv_test.setText("It ok, i'am binded");
}
```
Most helpful comment
To the author of the issue: if you really want to call ButterKnife#inject()
only from Base class, take a look at Activity#onPostCreate() or override
Activity#setContentView(), call super.setContentView() and then
ButterKnife#inject().
For Fragments you can use Fragment#onViewCreated().
Personally, I don't think that view binding in Base class is good idea.
29 мая 2015 г. 12:27 PM пользователь "Jake Wharton" <
[email protected]> написал: