I'm building a base activity for navigation and want something flexible so the Activity dictates to the Base Activity which layout to inflate.
I have the following
public abstract class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private int mLayoutRes;
protected void setLayout(int layoutRes) {
mLayoutRes = layoutRes;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(mLayoutRes);
// Layout implements toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null){
setSupportActionBar(toolbar);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// The layout implements the nav
if (drawer != null){
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
}
// Other Nav code ommitted as its too verbose
}
Then the Layout is passed from the activity as folows
public class Home extends BaseActivity {
private final String TAG = "Home";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.setLayout(R.layout.activity_home);
super.onCreate(savedInstanceState);
// Other Activity code
}
}
Is there a better way to achieve this?
Maybe setting a base layout with a content frame and inflating into that?
Any suggestions would be appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…