3

In a Bluetooth-related app (with minSdkVersion="18") I have a single MainActivity.java, displaying one of the following 3 UI Fragments:

  • MainFragment.java (the top screen)
  • SettingsFragment.java (settings screen, entered through menu)
  • ScanningFragment.java (lists nearby Bluetooth devices)

screenshot

To display an "Up button" and handle the "Back button" I have the following code in place:

public class MainActivity extends Activity 
                          implements BleWrapperUiCallbacks {

    // set in onResume() of each fragment
    private Fragment mActiveFragment = null;

´   @Override
    public void onBackPressed() {
        if (!getFragmentManager().popBackStackImmediate())
            super.onBackPressed();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_root);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            Fragment fragment = new MainFragment();
            getFragmentManager().beginTransaction()
                .replace(R.id.root, fragment, "main")
                .commit();
        }
    }

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

            case R.id.action_settings:
                Fragment fragment = new SettingsFragment();
                getFragmentManager().beginTransaction()
                    .addToBackStack(null)
                    .replace(R.id.root, fragment, "settings")
                    .commit();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

This works well, but has a cosmetic problem, that the "Up button" is still displayed when the MainFragment.java is being displayed - as you can see on the left side of the above screenshot.

I have tried calling

getActionBar().setHomeButtonEnabled(false);

when that fragment is being active, but that only disables the "Up button" - without really hiding it.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

3 Answers3

6

With help of Little Child (thanks!) here my solution using the FragmentManager.OnBackStackChangedListener:

public class MainActivity extends Activity 
                          implements OnBackStackChangedListener,
                                    BleWrapperUiCallbacks {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        getFragmentManager().addOnBackStackChangedListener(this);
    }

    @Override
    public void onBackStackChanged() {
        getActionBar().setDisplayHomeAsUpEnabled(
            getFragmentManager().getBackStackEntryCount() > 0);
    }
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • Using `MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener` from androidx, `getSupportActionBar()` works too equivalently. – John Sep 12 '21 at 21:27
4

You are in luck because in API level 18 there is this:

getActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);  

for support library, it is:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);  

So now depending upon what fragment you are in, you can change the icon of "up" button.

Also, try this:

getActionBar().setDisplayHomeAsUpEnabled(false);

Set whether home should be displayed as an "up" affordance.

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Do you know, how to hide the caret "<" character? – Alexander Farber Mar 17 '15 at 13:23
  • 1
    The last part. [`setDisplayHomeAsUpEnabled()`](https://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled%28boolean%29). – An SO User Mar 17 '15 at 13:23
  • 2
    Just a quick tip: if you ever want to change how the `ActionBar` looks, always have a look at `setDisplay*` methods. There is always something there. – An SO User Mar 17 '15 at 13:26
  • this helped me ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); – aida Apr 09 '17 at 08:53
0

2021 Working Solution:

(requireActivity() as AppCompatActivity).supportActionBar?.setHomeAsUpIndicator(null)
Sam Chen
  • 7,597
  • 2
  • 40
  • 73