8

This question has also been asked by someone on the Android Developers Google Group (link), but it does not have an answer...

I recently removed the v4 support library from my Android project in Eclipse, because my application only targets Android 4.0 (Ice Cream Sandwich) and up. I'm now going through all the support library references that are giving me errors.

One in particular is NavUtils, which seems to be a support library class used in Activity navigation. Example:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

What is the equivalent of NavUtils when not using the support library?

XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151

4 Answers4

4

Look at the source code of NavUtils, if necessary just copy it to your project. It just gets the parent activity set in the manifest and starts it.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • 1
    It's also useful to note that post ICS (JB and above) all the `NavUtils` functions are are incorporated into the `Activity` class. If you're targeting API 15 and above you sound't need `NavUtils` at all. – dcow Aug 05 '13 at 19:21
  • @DavidCowden you mean if you're targeting API 16 and above. 15 still needs NavUtils. – Yaroslav Mytkalyk Mar 01 '14 at 18:15
  • @DoctororDrive yes it's a typo. Thanks for catching it. – dcow Mar 05 '14 at 08:17
1

I found @Nikolay's approach to be the best if you don't want to have the whole android v4 support library in your project.

This is the list of classes you should copy into your project:

Christian García
  • 3,939
  • 1
  • 26
  • 26
0

Android version from Jelly Beans have the NavUtils incorporated into the Activity class.

There is no need to handle the case : android.R.id.home in onOptionsItemSelected(). OR you can just return false for this case in onOptionsItemSelected().

AndroidGuy
  • 1,270
  • 4
  • 15
  • 32
0

Here is this simplest answer I could find, and what I did in my code:

Simply replace:

NavUtils.navigateUpFromSameTask(this);

with:

navigateUpTo(getParentActivityIntent());

Hope this helps!

Codiak
  • 1,832
  • 2
  • 13
  • 20