Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable home button ActionbarSherlock, Sherlockfragment

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?

like image 411
user1534326 Avatar asked Jul 23 '12 17:07

user1534326


2 Answers

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.

like image 135
Cat Avatar answered Oct 17 '22 12:10

Cat


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.

like image 24
mattdonders Avatar answered Oct 17 '22 11:10

mattdonders