Action Bar With Back Arrow
Solution 1:
Add following line in your fragment if you want to show the back button from the fragment :
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
It would be better that along with doing this. You add the parent of the activity in the manifest file to make sure that parent activity is opened when back arrow is pressed.
Solution 2:
I have some thing like this for back button in toolbar instead of action bar.
In activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wolfmatrix.dummy.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarId"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/toolbarTextId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="14sp" />
</android.support.v7.widget.Toolbar>
<ImageButton
android:id="@+id/backButtonIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="left"
android:padding="10dp"
app:srcCompat="@drawable/ic_arrow_back_black_24dp" />
</RelativeLayout>
In styles.xml
: use theme => Theme.AppCompat.Light.NoActionBar
In ic_arrow_back_black_24dp.xml
, use this:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#ffffff"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
And now, toolbar has the back icon too.
Solution 3:
Try below code
Implement OnBackStackChangedListener and add this code to your Fragment Activity.
@Override
public void onCreate(Bundle savedInstanceState) {
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
//Handle when activity is recreated like on orientation Change
shouldDisplayHomeUp();
}
@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
}
@Override
public boolean onSupportNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
getSupportFragmentManager().popBackStack();
return true;
}
Solution 4:
add to your onCreate in the top
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
and add a function to the activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getSupportFragmentManager().popBackStack();
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
good luck
Solution 5:
Add below line after setcontentview()
//by doin that Back arrow will appear
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Create following overridden method after onCreate().
@Override
public boolean onSupportNavigateUp() {
finish();
return super.onSupportNavigateUp();
}
Post a Comment for "Action Bar With Back Arrow"