4

I have this problem with Google's Navigation Drawer 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 put mDrawerLayout.closeDrawer(mDrawerList); or any other code, it'll work normally.

There are no errors reported when the called activity is closed and no exception is thrown. Any thoughts?

Ron
  • 1,721
  • 1
  • 19
  • 43
  • Maybe you are calling finish() from the opening activity? What is parent? – JRomero Aug 15 '13 at 06:39
  • No, I'm not calling finish() anywhere. The parent is Dashboard.class. – Ron Aug 15 '13 at 06:44
  • So you are trying to open another activity instance of your current activity? – JRomero Aug 15 '13 at 06:45
  • Yes, that's correct. I'm trying to get to another activity using the navigation drawer. – Ron Aug 15 '13 at 07:04
  • If the parent is an instance of `Dashboard.class` which essentially should be the activity you are on (at best a subclass) and you are opening `Dashboard.class` then how would you know which one was the new vs the original? I guess what I'm trying to get at is are you sure it's doing what you are describing? – JRomero Aug 15 '13 at 07:08
  • For `case 0`, you should be seeing `Dashboard` activity? Is this not what's happening? – Vikram Aug 15 '13 at 07:09
  • Based on @J.Romero's comment, if you want to make sure that `startActivity(Intent)` did actually work: Show a `Toast` notification in your `onCreate(Bundle)` or if your Dashboard activity's launchMode is set to "singleTop", override `onNewIntent(Intent)` in Dashboard activity and show a `Toast`. If the Toast notification appears, `case 0` works. – Vikram Aug 15 '13 at 07:16
  • No, let's say I want to get to Card activity from Dashboard activity. I am able to run through all of the lines of the Card activity onCreate normally, then it stops and goes back to the Dashboard activity. – Ron Aug 15 '13 at 07:25
  • 2
    You should post the code for `Card` activity. – Vikram Aug 19 '13 at 01:00
  • It's basically a QR code generator. But the `Card` activity isn't the only activity that breaks the Navigation Drawer. ALL of them do (if I put them in `case 0:`, so posting it wouldn't help I'm afraid. – Ron Aug 19 '13 at 06:17
  • It is an `Activity` or `Fragment`. If this is fragment then use `startActivity(new Intent(getActivity(), Dashboard.class));` – Kartheek Sarabu Aug 23 '13 at 06:50
  • So you want to launch a new Activity of the same class as the Activity you are on? If the new Activity is the same as the old, how can you be sure that it didn't work? Side note, why would you even want to do this anyway? – Alex Fu Aug 23 '13 at 16:23
  • Why don't you use fragments instead of activities? – alecnash Aug 24 '13 at 13:30
  • I don't use fragments because I don't know how to use them yet and currently don't have the time to implement it. But I do plan to at a later version. @AlexFu: No, I want to launch any activity (including the current one) defined in the menu of the drawer. – Ron Aug 25 '13 at 07:38

1 Answers1

0

I tried reproducing this and it will not resolve parent. Do you have it declared somewhere else ?

What class are you using this in both Activities and Fragments can use startActivity() without the need for parent.startActivity()

Can you post the complete class?

This works for ok 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;

    }

}
chughes
  • 81
  • 5
  • parent is simply `TheActivity parent = this' on `onCreate'. So it's the same as calling 'this.startActivity(...)' – Ron Aug 25 '13 at 07:34