V
V
Vermut7562017-02-07 21:04:42
Java
Vermut756, 2017-02-07 21:04:42

I don’t understand how to intercept the home / up click in the v7 support lib if I have my own toolbar with ActionBarDrawerToggle?

Given: AppCompatActivity. With its own toolbar with DrawerLayout and a hamburger button. And a fragment in this activity, In a fragment at some point, the user opens a pop-up interface.
Required: implement interface closing in two ways - iron back and home/up button.
That is, when you open a hamburger, it changes to a back arrow. And when it is pressed, the pop-up interface is hidden, and the hamburger is returned again, which can open the DrawerLayout again.
With an iron back, I immediately noticed a problem. onBackPressed only works in activities. And here is a fragment, in order to transfer it - you need to fence something like your own listener, and this is not so simple because of the rather complicated lifecycle. You need to think carefully so as not to lead to pitfalls.
Therefore, I decided to postpone back for now. I took up the arrow, it seems to be easier: I already did it through onOptionsItemSelectedand android.R.id.home, and this should work in the fragment as well. How wrong I was (((
No, it works in the fragment. But it doesn’t work for another reason - because of the hamburger with DrawerLayout, which are made through the creation ActionBarDrawerToggle, but it changes everything, and onOptionsItemSelectedis not called at all when home / up is pressed, even after I called setDisplayHomeAsUpEnabled(true).Digging
through the forums and source code found the solution:

toolbar.setNavigationOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        // TODO Auto-generated method stub
      }
});

But this is again a headache, because the question immediately arises - where to put the old listener back?
Yes, and how to put it? More precisely, how to save the old listener to the buffer? I didn't find any get NavigationOnClickListener()
method . I decided to implement it using toolbar.getChildAt and searching for that Child, which is ImageButton, this is that button. But this is no longer a pure hack, and it also turned out that neither ImageButton nor View have a getOnClickListener method either! This is so incorporated in Android itself, and you can get around it only through - oh horror - reflection ... Curtain. It turns out that I first need toolbar.setNavigationOnClickListener, then re-create ActionBarDrawerToggle - because there is simply no other way to return the old handler - and where to do all this?
Logic suggests that in an Activity, declaring 2 corresponding methods, but accessing them from a fragment, is again a headache, because getActivity can return null, it can happen that the fragment is recreated, the pop-up interface disappears, but all this will not happen when pressing the arrow, and when Android wants to clear the memory, well, or something else, so instead of a hamburger there will always be an arrow ...
No words. Only emotions. Hatred. Tons of hatred for those who wrote the lib support and the vile ActionBarDrawerToggle. Why did they break such a convenient onItemSelected, available in fragments, and did not give anything adequate in return? Why???
My head is already swollen, and I still have to do an iron back ....
OnBackPressed is also "good", why is it not in fragments, even the v4 version of the support, and even a newer version of the base class for the fragment simply does not exist ??? What was difficult for them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-02-07
@Vermut756

Smoke the life cycle of fragments and interact through activities. Hang a listener on the drover, which will call onBackPressed from the activity, and it already passes into fragments (through its BaseFragment base class or the type from which all fragments inherit).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question