Enable home button ActionbarSherlock, Sherlockfragment
Asked Answered
P

3

5

I want to enable the home button in my fragment. This question is asked earlier but for an activity.
I tried ...

 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

... but this doesn't work.
Here is my code:

import com.actionbarsherlock.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.MenuItem;

import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class SafanTab extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.safantab, container, false);
    }

    public OnClickListener onOverClick = new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
            startActivityForResult(myIntent, 0);
        }
    };

    public OnClickListener onProductenClick = new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
            startActivityForResult(myIntent, 0);
        }
    };

    public OnClickListener onTwitterClick = new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
            startActivityForResult(myIntent, 0);
        }
    };

}

How can you enable a home button on SherlockFragment?

Pasha answered 23/7, 2012 at 17:24 Comment(0)
H
13

Have you tried adding the below code the main Activity - the one that extends SherlockActivity or extends SherlockFragmentActivity?

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

I don't think you can getSupportActionBar in something that only extends SherlockFragment.

Hosfmann answered 23/7, 2012 at 17:39 Comment(1)
But SherlockFragment does allow you to call getSherlockActivity().getSupportActionBar().Maddux
T
25

You also need to override the options menu selection:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Keep in mind, the code above would run in an activity (hence finish()). If you don't use an Activity (which would be odd to me...), then you'll need to replace that.

Tip answered 23/7, 2012 at 17:35 Comment(8)
but it is giving an errot at "getSupportActionbar(0)"Pasha
...then say that in your original post. Try changing it to getSherlockActivity().getSupportActionBar().Tip
Thanks for the answer but mattdonders has giving the right answer.Pasha
I read here: developer.android.com/guide/topics/ui/actionbar.html#Home that override the option menu selection is not required. So, why it does not work without this overriding?Ray
@Joseph82 From that documentation: Now the icon in the action bar appears with the Up caret (as shown in figure 4). However, it won't do anything by default. To specify the activity to open when the user presses Up button, you have two options... Basically, they show different methods for overriding the home button. Regardless, that button does nothing without telling it what to do.Tip
Yes @Eric, but I can't see in the documentation your solution. I have tried with the first solution and it does not workRay
@Joseph82 At the risk of potentially stating the obvious, not every solution is in the documentation. (That's why Stack Overflow exists.) As for the solutions that are in the documentation, if they're not working for you, consider asking a new question with your attempts and what's not working.Tip
@Eric take a look at my answer, you'll understand what I was trying to say.Ray
H
13

Have you tried adding the below code the main Activity - the one that extends SherlockActivity or extends SherlockFragmentActivity?

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

I don't think you can getSupportActionBar in something that only extends SherlockFragment.

Hosfmann answered 23/7, 2012 at 17:39 Comment(1)
But SherlockFragment does allow you to call getSherlockActivity().getSupportActionBar().Maddux
R
0

The simplest solution is to follow these three simple steps:

1 - Declare in the AndroidManifest which is the parent of your activity:

<activity android:theme="@style/Theme.MyApp"
            android:name="com.example.CurrentActivity" 
            android:parentActivityName="com.example.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.ParentActivity" />

2 - Add the Up icon in the action bar, simply calling from your onCreate method:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

3 - Specify the activity to open when the user presses the Up button:

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

NOTE: If you call (as suggested by @Eric):

finish();

instead of:

NavUtils.navigateUpFromSameTask(this);

your are not really performing "up navigation", since as the documentation says:

Up navigation is distinct from the back navigation provided by the system Back button. The Back button is used to navigate in reverse chronological order through the history of screens the user has recently worked with. It is generally based on the temporal relationships between screens, rather than the app's hierarchy structure (which is the basis for up navigation).

Ray answered 18/10, 2013 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.