Equivalent of NavUtils when not using the Android support library?
B

4

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?

Brothers answered 18/12, 2012 at 6:9 Comment(0)
S
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.

Slugabed answered 18/12, 2012 at 6:20 Comment(3)
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.Edelweiss
@DavidCowden you mean if you're targeting API 16 and above. 15 still needs NavUtils.Scrummage
@DoctororDrive yes it's a typo. Thanks for catching it.Edelweiss
D
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:

Damage answered 8/7, 2013 at 12:46 Comment(0)
F
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().

Famagusta answered 16/4, 2015 at 9:8 Comment(0)
B
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!

Bruxelles answered 22/10, 2015 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.