Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock: OnOptionsItemSelected doesn't recognize R.id.home

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?

like image 437
buggydroid Avatar asked Mar 22 '13 08:03

buggydroid


3 Answers

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.

like image 116
Dhaval Parmar Avatar answered Nov 16 '22 03:11

Dhaval Parmar


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.

like image 33
Frank Avatar answered Nov 16 '22 03:11

Frank


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

like image 32
brendosthoughts Avatar answered Nov 16 '22 03:11

brendosthoughts