I have a ViewPager
with Fragment
s. When a button is clicked in the Fragment
, I launch an Activity
on mine. Pressing the back button on my phone when I'm in the Activity
, takes me back to my previous screen i.e. the one with the Fragment
s in the ViewPager
.
I'd like to enable the "up" button in my ActionBar
and in order to do so, I've written the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
Now the "up" button is shown, but clicking it doesn't take me back to the previous screen. I'd like the "up" button to the same thing as the "back" button.
How can I do this? What am I doing wrong?
Thanks.
Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button. Custom the back event at onOptionsItemSelected. This will enable the back function to the button on the press.
This example demonstrates How to get action bar tittle in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
You need also to implement what should be done when the up button is clicked:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With