Enable home button ActionbarSherlock, Sherlockfragment

偶尔善良 提交于 2019-12-19 19:39:45

问题


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?


回答1:


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.




回答2:


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.




回答3:


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).



来源:https://stackoverflow.com/questions/11617399/enable-home-button-actionbarsherlock-sherlockfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!