Butterknife: How to use when a AnotherActivity extends baseActivity?

Created on 27 Jan 2016  路  13Comments  路  Source: JakeWharton/butterknife

How to use "ButterKnife.bind(this);" when a AnotherActivity extends BaseActivity?
I've already override the setContentView method in baseactivity, when I use "ButterKnife.bind(this);" in AnotherActivity , it will throws "Caused by: java.lang.IllegalStateException"

Most helpful comment

In my base activity I'm actually using this:

public abstract class BaseActivity extends RxAppCompatActivity {
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
    }
}

All 13 comments

using this for your baseActivity :

public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
    }

    public abstract @LayoutRes int getLayout();
}

for each activity you must return layout id by getLayout

If I do this ,how could i set contentView of BaseActivity?

In my base activity I'm actually using this:

public abstract class BaseActivity extends RxAppCompatActivity {
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
    }
}

It's still not works, my BaseActivity has DrawerLayout, dou you have a good way to add it to BaseActivity and other activity can extends BaseActivity?

If you call bind(this) at BaseActivity, you don't need to call bind(this) at AnotherActivity.
If AnotherActivity does not have DrawerLayout which BaseActivity has, add @Nullable to that view.
If AnotherActivity have DrawerLayout, you don't need to declare DrawerLayout in AnotherActivity.

I don't get it, I think the drawer layout should be in an activity that extends your base activity:
Base Activity

public abstract class BaseActivity extends RxAppCompatActivity {
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
    }
}

Then in your MainActivity that should have the drawer layout:

public class MainActivity extends BaseActivity implements
        NavigationView.OnNavigationItemSelectedListener{

    @Bind(R.id.drawer_layout) DrawerLayout mDrawerLayout;
    @Bind(R.id.toolbar)Toolbar toolbar;

    @Bind(R.id.search_view) MaterialSearchView mSearchView;
    @Bind(R.id.nav_view) NavigationView mNavigationView;
    private SmoothDrawerListener mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        assert ab != null;
        ab.setHomeAsUpIndicator(R.drawable.ic_person_outline_white_24dp);
        ab.setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setIcon(R.drawable.zero_dimen_icon);

        mNavigationView.setNavigationItemSelectedListener(this);

        mDrawerToggle = new SmoothDrawerListener(this, mDrawerLayout, toolbar);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

And all is working just fine

I mean all of my activitieswant to use the same drawerLayout,so I made a BaseActivitywho has a drawerLayoutandimplements NavigationView.OnNavigationItemSelectedListener, when anotherActivitywant to have DrawerLayout, I just let it extends BaseActivity,
What should I do ? Every activity has _different_ contentView.

Ah okay, so you want your BaseActivity to have a different layout res than your OtherActivity ?

So you can write @Bind(R.id.drawer) DrawerLayout drawer and call bind(this) at BaseActivity,
then you can use drawer at AnotherActivity.
You don't need to do extra (like call bind()) at AnotherActivity.

:+1: Exactly, just that you need to have R.id.drawer defined in the layout of AnotherActivity

This seems to be answered, but isn't a bug or feature request anyway so closing. StackOverflow is a great place for asking usage-based questions since it serves as an archive for future people searching for answers. Much better than GitHub issues in that regard.

@sohayb @ytRino @Tneciv you have to write Butterknife.bind(this) in both Base and Extended Activities. But in Base Activity, you should annotate binded fields with @Nullable like:

@BindView(R.id.base_content_frame) @Nullable

Related issue: https://github.com/JakeWharton/butterknife/issues/343

_I think you need to put @Nullable on both Base and Extended Activity binded fields, then it works. THats a big boilerplate :/_

on BaseActivity

 @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
    }
Was this page helpful?
0 / 5 - 0 ratings