Answer the question
In order to leave comments, you need to log in
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 onOptionsItemSelected
and 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 onOptionsItemSelected
is 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
}
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question