Java – Drawer navigation does not work when using startActivity in the first case of selectItem

Drawer navigation does not work when using startActivity in the first case of selectItem… here is a solution to the problem.

Drawer navigation does not work when using startActivity in the first case of selectItem

I’m having this issue with Google’s Navigation Lader, where starting the activity specified in the first case (case 0) in my selectItem method breaks and returns to the previous activity.

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, 
                            int position, long id) {
            selectItem(position);
    }
}

private void selectItem(int position) {
    switch(position) {
    case 0:
         Placing any startActivity here will load the activity
         but immediately return to the calling activity.
        parent.startActivity(new Intent(parent, Dashboard.class));                  
        break;
    case 1:
        parent.startActivity(new Intent(parent, Card.class));
        break;
    }
}

But if I type mDrawerLayout.closeDrawer(mDrawerList); or any other code, and it will work.

When the invoked activity is closed, no error is reported or an exception is thrown. Any ideas?

Solution

I tried to reproduce this, but it won’t resolve the parent. Did you declare it elsewhere?

Any class you use in activities and fragments can use startActivity() without parent.startActivity().

Can you publish a complete class?

That’s fine for me.

private void selectItem(int position) {

switch (position) {
    case 0:
         goto home screen
        Log.d(TAG, "Showing Home");

startActivity(new Intent(this, SettingsActivity.class)); 
        break;

case 1:
         Show Editor
        Log.d(TAG, "Showing Editor");

break;

default:

break;

}

}

Related Problems and Solutions