Reputation: 55
I am building an android app and I want all the activities (screens) to have a single navigation drawer (which contains an icons besides the headings) to do so I found that its possible with inheritance and I tried so many solutions that were provided here but I could not solve the hard part ( handling different layouts )
Upvotes: 0
Views: 1392
Reputation: 8836
I had to do something like that but we were using fragments, so it was pretty straightforward for me. It might help you.
You need a base activity layout
// res/layout/activity_base.xml
<!-- parent layout -->
<FrameLayout
android:id="@+id/container"
</FrameLayout>
<!-- drawer layout and other views -->
In base activity which contains drawer layout handle all code for drawer layout and also has a method to set the fragment.
public class BaseActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstance){
setContentView(R.layout.activity_base.xml);
// your rest of the code including drawer layout
}
}
protected void setFragment(Fragment f){
getSupportFragmentManager.beginTransaction()
.replace(R.id.container,f)
.commit()
}
For the other activity which wants to have drawerlayout, simply extends BaseActivity and set the fragment by using setFragment. The point is FooActivity doesn't have its own activity layout, it uses BaseActivity layout and put its own fragment.
public class FooActivity extends BaseActivity{
@Override
protected void onCreate(Bundle
setFragment(FooFragment.newInstance());
}
}
Upvotes: 1