1

My app is set up to have a unique toolbar for each Fragment. In one particular fragment, I want to override the Navigate Up or Back button in the toolbar to give a warning to the user to confirm their intention.

I originally asked this question here and found that by changing my original code to using setSupportActionBar to implement the toolbar, I was able to maintain my unique toolbar for the fragment and ovveride the Navigate Up button.

However, I just noticed that whenever I back out of that fragment that uses setSupportActionBar for the toolbar, I get a memory leak (same as the problem found by this user). I confirmed this by commenting out the line that sets up the actionbar and saw that the leak had disappeared.

How can I maintain my unique toolbar, override the Navigate Up button and avoid this memory leak?

Don Robin
  • 121
  • 2
  • 16

1 Answers1

1

However, I just noticed that whenever I back out of that fragment that uses setSupportActionBar for the toolbar, I get a memory leak

You can try to eleminate this by setSupportActionBar(null) when this particular fragment is destroyed:

override fun onDestroy() {
    super.onDestroy()
    (requireActivity() as AppCompatActivity).setSupportActionBar(null)
}

Java:

// In the fragment
@Override
public void onDestroy() {
    super.onDestroy();
    ((AppCompatActivity) requireActivity()).setSupportActionBar(null);
}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thanks again, friend. This seems to work – Don Robin Sep 30 '21 at 23:09
  • Actually, I noticed after backing out of that fragment and trying to re-enter, I get a Null pointer exception error: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference. Any ideas? – Don Robin Sep 30 '21 at 23:22
  • @DonRobin where do you call `toolbar.setTitle()` ? ... I think it's called before re-initializing the toolbar again as `supportActionBar` – Zain Sep 30 '21 at 23:24
  • I navigated and set the title of the toolbar using navigation component. I removed `setupWithNavController(findNavController())` before because I wasn't able to ovveride the "Navigate Up" button with it being there. However, the title for the toolbar updates correctly when opening the fragment the first time. What would be the most efficient workaround for this? – Don Robin Oct 02 '21 at 19:29
  • @DonRobin Not sure if you are setting up the toolbar automatically using [`setupWithNavController()`](https://developer.android.com/reference/androidx/navigation/ui/NavigationUI#setupWithNavController(androidx.appcompat.widget.Toolbar,androidx.navigation.NavController,androidx.navigation.ui.AppBarConfiguration)).. If so then you can just set the fragment names in the navGraph, and the navigation components will change it automatically during the navigation without needing to explicitly use `setTitle()` – Zain Oct 02 '21 at 19:56
  • 1
    That's correct, I am not using `setTitle()` but instead relying on navigation components to set the title of the toolbar. I think the problem may stem from combining use of `setupActionBar` and navigationcomponents. I was able to get it to work by removing setupActionBar and listing this fragment as a top level destination. As a result, I realized I can easily override the navigate up button without any leaks. – Don Robin Oct 02 '21 at 21:25
  • Nevermind, I don't even need to make it a top level destination. I just needed to use `setNavigationOnClickListener` in my fragment and I can override the navigate up function very easily – Don Robin Oct 02 '21 at 21:39