1

In my actvity, I created a ActionBar, used a back icon, its showing in the app, but on clicking back icon nothing is happening, didn't got the function onnavigationiconclick event working also, below is my code, kindly assist so that on clicking the back icon user can be send to MainActivity --

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab1gridlatestvideos);

        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                | ActionBar.DISPLAY_SHOW_CUSTOM);

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Latest Videos");
}

3 Answers3

1

Inside your activity

getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true);

@Override
public boolean onSupportNavigateUp() {
 onBackPressed();
 return true;
}
Mahesh Vayak
  • 1,056
  • 12
  • 25
0

In the manifest xml, set this attribute for the activity from where you want to navigate up:

android:parentActivityName="com.example.MainActivity"

Set the value to the activity you want to navigate back to.

Add this meta data tag for supporting android versions 4.0 or lower:

<meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />

For information at: Providing Up Navigation

Note: Since you have used ActionBar object, make sure you are using the right one, that is android.support.v7.app.ActionBar

Pulak
  • 768
  • 7
  • 16
0

try using a custom toolbar in activity.xml

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

1- set your toolbar

Toolbar toolbar = findViewById(R.id.toolbar);

2- setup your icon

if (toolbar != null) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

}

3- override this method

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}
Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29