I am making an app from the example of Android Developers with the Navigation Drawer. I made the items but I don\'t know how I can open new Activity from each of items enlis
switch(position) {
case 1:
Intent a = new Intent(Book.this, SecondActivity.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(Book.this, ThirdActivity.class);
startActivity(b);
break;
default:
}
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
SelectItem(position);
}
}
Instead of using Activities just use a fragment and A fragment Manager to transition between them
Instead of your code:
private void selectItem(int position) {
Fragment fragment = new GalaxyFragment();
Bundle args = new Bundle();
args.putInt(GalaxyFragment.ARG_Galaxy_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mGalaxyTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
Use this code:
private void selectItem(int position) {
switch(position) {
case 1:
Intent a = new Intent(MainActivity.this, Activity1.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(MainActivity.this, Activity2.class);
startActivity(b);
break;
default:
}
}
And remove this part: (It is not necessary)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 1:
Intent a = new Intent(MainActivity.this, Page1.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(MainActivity.this, Page2.class);
startActivity(b);
break;
default:
}
}
In manifest you write this code:
<activity
android:name=".firstactivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
In Main activity:
Intent i=new Intent(MainActivity.this,firstactivity.class);
startActivity(i);
Use Like that
switch (position) {
case 0:
fragment = new Free();
break;
case 1:
fragment = new Manage();
break;
case 2:
fragment = new Paid();
break;
case 3:
fragment = new Categories();
break;
case 4:
logout();
break;
default:
break;
}