The action bar home up button is the button on extreme left of action bar. Its functionality is to navigate the app to the parent activity. The functionality can be achieved by two ways.
Method 1: By adding it in Android Manifest
1 |
android:parentActivityName="<YOUR_PACKAGE>.<PARENT_ACTIVITY_NAME>" |
In the activity’s <activity> tag of Android Manifest file add the tag. For example I have two activities, activity1 and activity2. And i want to go to activity1 on click of activity2’s home up button then my Android Manifest file will be like
1 2 3 4 5 6 7 8 |
<activity android:name=".activity1" </activity> <activity android:name=".activity2" android:parentActivityName="com.example.myapp.activity1" > </activity> |
Method 2: By adding check in onOptionsItemSelected()
You can also implement a check on Home up button ID in onOptionsItemSelected() method as
1 2 3 4 5 6 7 8 9 10 11 |
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if(id == android.R.id.home) { startActivity(new Intent(activity2.this,activity1.class); return true; } return super.onOptionsItemSelected(item); } |
Both methods works well but if you want to give the home up button the functionality of back press then go for second method else you can use whatever suits you.
Be the first to comment.