Skip to content Skip to sidebar Skip to footer

Back Button Is Not Working Properly In Right Side Of Action Bar

I want to just add back button in Right side of action bar and I found many link for this. This is my code which is app->res->menu->main.xml

Secondly, it's not very easy to understand what's happening in your code, but as far as I see, you HAVE NOT declared in the manifest.xml the parent elements.

This is a nice and simple example:

<!-- Parent activity meta-data to support 4.0 and lower --><meta-dataandroid:name="android.support.PARENT_ACTIVITY"android:value="com.example.myfirstapp.MainActivity" />

A parent activity definition can EASE your life. This way you can define which activity will be loaded on UP NAVIGATION interaction.

Hope that helps :)

Solution 2:

First of all i wanted to say what you are trying to do is not a good practice, why would you want a back button in your action bar if Android devices already have one on the bottom of the screen and you can turn on Up-Navigation at the left side of the Action Bar?

Custom App-Navigation clutters your code, confuses users and most of the time is just ugly.

But if you really need a button that does the navigation, you want it to access the standard android navigation methods.

For Back Navigation:

Just use the standard android back action onBackPressed().

publicbooleanonOptionsItemSelected(MenuItem item) {

   if (mDrawerToggle.onOptionsItemSelected(item)) {
        returntrue;
    }

    switch (item.getItemId()) {

        case R.id.backAction:
            onBackPressed();
            returntrue;

        default:
            returnsuper.onOptionsItemSelected(item);
    }

 }

If this function doesn't do what you want, you can just override it with your own wanted behavior.

For Up Navigation:

publicbooleanonOptionsItemSelected(MenuItem item) {

   if (mDrawerToggle.onOptionsItemSelected(item)) {
        returntrue;
    }

    switch (item.getItemId()) {

        case R.id.backAction:
            NavUtils.navigateUpFromSameTask(this);
            returntrue;

        default:
            returnsuper.onOptionsItemSelected(item);
    }

 }

You will have to configure your parent activites in the manifest, but because of that the behavior of this Navigation is easier to change.

Post a Comment for "Back Button Is Not Working Properly In Right Side Of Action Bar"