Android fragment toolbar back button

Back navigation with Fragments / Toolbar

I’m scratching my head with this one now. I have an ActionBarActivity that loads an initial Fragment — the original menu is inflated within the activity. Now, I have a navigation bar that, when an item is selected, loads a different fragment and adds this to the backstack.

When I do this, there are a couple of things I want to set:

  1. Set the home as up indicator
  2. Invalidate the options menu from the main activity
  3. Set has options to true for the Fragment
  4. Ensure that the up indicator correctly navigates back to the original Fragment

Something rather strange is going on — the up indicator appears once only and does not behave as the back button and although I’ve invalidated and inflated a new menu, the new menu gets appended to the original Activity menu.

EDIT: Ok I’ve resolved the appending issue — forgot to add menu.clear() in the onCreateOptionsMenu method.

My navigation drawer layout has onClick methods to all menu items which would trigger the load of another Fragment:

Once the item is triggered the onCreate contains the invalidate and the hasOptions code:

The onCreateOptionsMenu then inflates another menu layout (contains a single item called settings).

As mentioned, this only partially works once — the first time I use the item to load the Fragment, I get the back icon but it’s also not working (this is set within onOptionsItemSelected to check for the home item press — it does nothing). When I press the back button it takes me back to the correct place. If I navigate back however, the back arrow now longer shows even though the code runs through onCreate !

Источник

How to add «UP» back button in fragment (Fragment to Activity)

I want to go from fragment to activity using back button using toolbar back icon.

The fragment is my navigation drawer item & activity is my MainActivity.

5 Answers 5

You can use app:navigationIcon=»?attr/homeAsUpIndicator» for that back navigation icon.

Call this method in your fragment onCreateView

Try this worked for me :

Create an back arrow icon in drawable folder. Name it ‘ic_back_button’. Not sure how :-
just right click on drawable > new > ImageAsset > Clip Art > Search back > select > OK > Finish (don’t forget to change the name).

Читайте также:  Блокировка экрана для андроид замок

then Inside your fragment in onCreateView :

Add this xml code to your fragment and try

You can easily do that, if you are using a Custom back button that is placed on your Custom top app bar, in the button’s onClick() function you can call.. getActivity().onBackPressed(); it would work the same as if you have clicked the android navigation’s back button.

Источник

Android Fragment handle back button press [duplicate]

I have some fragments in my activity

And on Back Button Press I must to return from [2] to [1] if current active fragment is [2], or do nothing otherwise.

What is the best practise to do that?

EDIT: Application must not return to [2] from [3]. [6]

25 Answers 25

When you are transitioning between Fragments, call addToBackStack() as part of your FragmentTransaction :

If you require more detailed control (i.e. when some Fragments are visible, you want to suppress the back key) you can set an OnKeyListener on the parent view of your fragment:

I’d rather do something like this:

if you overide the onKey method for the fragment view you’re gonna need :

Use addToBackStack method when replacing one fragment by another:

Then in your activity, use the following code to go back from a fragment to another (the previous one).

If you want to handle hardware Back key event than you have to do following code in your onActivityCreated() method of Fragment.

You also need to check Action_Down or Action_UP event. If you will not check then onKey() Method will call 2 times.

Also, If your rootview(getView()) will not contain focus then it will not work. If you have clicked on any control then again you need to give focus to rootview using getView().requestFocus(); After this only onKeydown() will call.

Источник

Android Toolbar Back Button Tutorial

Hi and welcome to another tutorial from codingdemos, today you will learn how to create android toolbar back button for your app.

You can use the android toolbar back button to navigate from one screen to another without having to tap on the actual device back button. Let’s get started 🙂

In this tutorial we will be using the following:

  • Android studio version 2.3.3
  • Android emulator Nexus 5X with API 24
  • Minimum SDK API 16
  • Open up Android Studio and create a new project and give it a name, in our case we’ve named it (ToolbarButtonTutorial), choose API 16 as the minimum SDK, then choose a blank activity and click on finish and wait for Android Studio to build your project.
  • Open (styles.xml) file and change the parent theme from (Theme.AppCompat.Light.DarkActionBar) to (Theme.AppCompat.Light.NoActionBar) like this:
  • Next let’s add android toolbar, open (activity_main.xml) file and make the following code changes
  • Now let’s add the back arrow icon inside android toolbar by using Android asset studio. Click on res and then right click on drawable=>New=>Vector Asset.
Читайте также:  Mimitos андроид много денег

Android studio asset studio (Large preview)

  • Click on icon where the red arrow is pointing and choose the back arrow icon from the icons list and then click on finish
  • When you open (ic_arrow_back_white_24dp.xml) file you will see the following code:
  • Change the color of the back arrow icon from black to white by adding white color code inside (android:fillColor) like this:
  • Open (MainActivity.java) file and let’s define the toolbar, set the app name to be shown in the toolbar as well as the back arrow icon like this:
  • Run the app, and you will be able to see the toolbar with app name and the back arrow icon.

Android Toolbar Back Button (Large preview)

  • Now let’s make the back arrow icon clickable so that when you tap on the icon, you will exit the app like this:

In this tutorial we’ve built a simple android app that uses android toolbar back button, to allow the user to navigate between the screen(s) without using the actual device back button. The source code for android toolbar button is available on Github, and if you have a question(s), please let me know in the comment section, and I’ll do my best to answer them.

Источник

Toolbar — Switching from drawer to back button with only one Activity

I’ve been searching for a while on how to change between the drawer open/close icon (going from a hamburger to the arrow) to a simple back arrow. My application at the moment only has one Activity which switches between several fragments. At one point, I want to transition between one of the main fragments (ie, one of the fragments in the drawer) to a fragment that hierarchically is under the previous fragment (ie, an «Add New » fragment). In this new fragment, I want to have the Toolbar to show the back button instead of the drawer button.

Читайте также:  Обзор китайского смартфона android

I’ve been looking around and trying different solutions for quite a while. Here are the most notable:

  • Change drawer icon back to back arrow — I successfully removed the drawer icon, but in place there’s. nothing. No up caret, no back button, no icon. I suspect this is because my Activity has no parent, but other than a cheap work around (create another Activity that acts as a parent which launches the main Activity), I’m at a lost of what to do.
  • Switching between Android Navigation Drawer image and Up caret when using fragments — Similar to the above, yet has far more detail. Ultimately, the icon still doesn’t turn into a back button.
  • Android lollipop toolbar switch between open/close drawer and back button — I find this hard to follow, but ultimately the drawer icon can be tapped and does nothing (although I believe I know how to make it act as a back press). However, the icon doesn’t change.

At the moment, I’m thinking of a long, arduous method of creating a custom icon that I hide and show (and hide/show the native drawer icon). However, is there a better way to switch between the drawer and back buttons?

As a side yet related question, I’ve been looking at the Material Design docs, and a few examples have an X in the top left corner. How different is that to implement than implementing the drawer vs back/up buttons?

Edit:

I can figure out how to replace the icon, but how would I get the click event?

So far, this was my best lead:

What I’ve tried now:

  • Disabled the DrawerToggle when necessary (ie, mDrawerToggle.setDrawerIndicatorEnabled(useDrawer); )
  • Added logs in onOptionsItemSelected in my NavigationDrawerFragment, my Activity, as well as the DialogFragment I’m currently testing which run if item.getItemId() == android.R.id.home is true. None of these log statements go off

For better context, I now have a full screen fragment which adds a «Save» button to the menu and changes the drawer icon to an «X». The fragment can get the save menu event, yet not even the Activity and Drawer can get when the X is tapped.

Edit2:

As requested, here is some code. Note that this is all from this Github repo, which I’m actively working on (note that I have a few useless functions here or there from rapid testing).

Solution:

This is the ultimate solution I ended up on, with the help of natario’s answer below:

Источник

Оцените статью