10

I'm using the ActionBarSherlock library and I'm following the exact steps as suggested here and here to enable navigation to the previous screen.

My code looks like this:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // This callback is used only when mSoloFragment == true (see
    // onActivityCreated above)
    switch (item.getItemId()) {
    case android.R.id.home:
        // App icon in Action Bar clicked; go up
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Reuse the
                                                            // existing
                                                            // instance
        startActivity(intent);

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

But R.id.home is not recognized and home shows up in red. :-/ If I use the native actionbar the home declaration takes me to ids.xml file. But here the declaration is not found while I use the ActionBarSherlock Activity. Am I missing something?

Community
  • 1
  • 1
buggydroid
  • 1,870
  • 6
  • 25
  • 36

3 Answers3

12

just replace this

android.R.id.home

to

R.id.home

and check your code... run it

because

R.layout.* are layouts you provide (in res/layout, for example).

android.R.layout.* are layouts that ship with the Android SDK.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
7

I know this is an old question but I believe the right answer is missing.

It should be be android.R.id.home because it is a platform resource, so your code is fine.

Make sure your minSdkVersion is 11 or higher since home was introduced in 11.

Frank Rem
  • 3,632
  • 2
  • 25
  • 37
1

I remeber running into this problem and apparently its quite frequent a quick google or search through stack overflow should've given you some insight anyways check this thread out R cannot be resolved - Android error Im pretty sure your running into same problem

Community
  • 1
  • 1
brendosthoughts
  • 1,683
  • 5
  • 21
  • 38
  • 4
    Brendan - My problem is not with the R error. It's concerned with ActionBarSherlock library. Try to be more specific with your answers. :) Just a suggestion. Thanks anyway. – buggydroid Mar 22 '13 at 09:05