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