Display Back Arrow on Toolbar

Display Back Arrow on Toolbar

If you are using an ActionBarActivity then you can tell Android to use the Toolbar as the ActionBar like so:

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

And then calls to

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

will work. You can also use that in Fragments that are attached to ActionBarActivities you can use it like this:

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

If you are not using ActionBarActivities or if you want to get the back arrow on a Toolbar that's not set as your SupportActionBar then you can use the following:

mActionBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//What to do on back clicked
}
});

If you are using android.support.v7.widget.Toolbar, then you should add the following code to your AppCompatActivity:

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}

How to display and set click event on Back Arrow on Toolbar?

First make one toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

then include it in activity_main.xml like this way:

<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />

</LinearLayout>

then in your MainActivity.java file, put this code:

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("MyTitle");

To add listener on back press, use following method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// todo: goto back activity from here

Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

How to implement android Toolbar Back button

You can handle back icon very easily. If all of your fragment are in single Activity I really recommend to handle this with following way :

first crate a abstract BaseFragment class which implement FragmentManager .OnBackStackChangedListener then put following method inside that :

  @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mainActivity = (MainActivity) getActivity();

getFragmentManager().addOnBackStackChangedListener(this);

shouldDisplayHomeUp();
}

@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}


public boolean shouldDisplayHomeUp() {
//Enable Up button only if there are entries in the back stack
boolean canBack = false;
try {
canBack = getFragmentManager().getBackStackEntryCount() > 0;
} catch (Exception ex) {
// Log.e(getClass().getCanonicalName(), ex.getMessage());getMessage
}

if (canBack) {
mainActivity.drawerDisable();
} else {
mainActivity.drawerEnable();
}
return canBack;
}

By this way disableDrawer & enableDrawer function handle your Icon and OnBackPressed method handle your BackStack Now in your activity when you press back-icon display if needed. your onBackPressed should be something like this :

 int backStackCount = getSupportFragmentManager().getBackStackEntryCount();

if (backStackCount == 0) {
//nothing exist in backStack OS handle it
super.onBackPressed();
} else {

getSupportFragmentManager().popBackStack();
}

See full implementation here.

(Navigation component) How to display back arrow on the activity when back to the home fragment?

As per the setupWithNavController(Toolbar, NavController) documentation:

The start destination of your navigation graph is considered the only top level destination. On all other destinations, the Toolbar will show the Up button.

If you want to also show the Up button on your start destination (i.e., to go to the previous activity), you'd want to use the version that takes an AppBarConfiguration.

As per the Update UI components documentation on AppBarConfiguration, AppBarConfiguration allows you to set exactly what destinations you want as top level destinations. To get the Up button to show on every destination, you'd use an empty set of top level destinations:

AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder().build();

Note that since you're using setSUpportActionBar(), you should follow the Action Bar documentation and use the setupActionBarWithNavController() method rather than the Toolbar version. You must also override onSupportNavigateUp() to handle the up button.

Therefore your complete code would look like:

public class EditServicesActivity extends BaseActivity<MainViewModel> {


public Toolbar toolbar;
public AppBarConfiguation appBarConfiguation;
public NavController navController;

@Override
protected void initActivityComponent() {
component = ProServeTechApp.getComponent(this)
.plus(new ActivityModule(this));
component.inject(this);
}

@Override
protected int getLayout() {
return R.layout.activity_edit_services;
}

@Override
protected Class<MainViewModel> getViewModelClass() {
return MainViewModel.class;
}

@Override
protected void initActivity() {
viewModel.getRequestDetails(getIntent().getExtras().getString("requestId"), String.valueOf(0));
viewModel.getIssues(getIntent().getStringExtra("requestId"));

toolbar = findViewById(R.id.toolbar_hesham);
setSupportActionBar(toolbar);

navController = Navigation.findNavController(this, R.id.nav_host_edit_service);
appBarConfiguration = new AppBarConfiguration.Builder().build();
NavigationUI.setupActionBarWithNavController(this, navController,
appBarConfiguration);
}

@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}

How to display back button on action bar in android another activity

Just add code this in the onCreate method of your [CurrentActivity].java file.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

And this line of code will just add a back button in your Action Bar, but nothing would happen after tapping that right now.

And add this in your [CurrentActivity].java, this will add the working of that button:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:

Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
startActivity(intent);
finish();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

And replace CurrentActivity to your activity name and replace MainActivity to the activity you want to send user after pressing back button



Related Topics



Leave a reply



Submit