Question

I have created several fragments and I add the first fragment the following way:

mainFragment = (MainFragment) MainFragment.create();
    getSupportFragmentManager().beginTransaction()
    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
    .add(R.id.content, mainFragment, MAIN_FRAGMENT_TAG)
    .commit();

The second fragment is added this way:

     getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
    //.hide(mainFragment)
    .add(R.id.content,VenueFragment.create(vid), "Venue Fragment")
    .addToBackStack(null)
    .commit();
    setDrawerIndicatorEnabled(false);

Now as you see the hide method is not applied for this transaction, and the VenueFragment is opened, the problem in this case is that while the VenueFragment opened (and it's a full screen view) pressing on empty section of this fragment invokes calls of the MainFragment clickable views. How can I prevent this?

If I use the hide option then it's not happening but for some reason the animation for removing the MainFragment is going up and that makes a weird experience.

Was it helpful?

Solution

You need to make the root ViewGroup of VenueFragment clickable so it handles click events and they do not pass down (in the z-order sense) to the other Fragment.

OTHER TIPS

Set clickable property on the second fragment's view to true.

For Example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true" />

Setting an onClickListener on VenueFragment will work, but I'll add a little bit more explanation. Having the main fragment get a touch event is intended behavior to handle the case where your venue fragment is transparent. In that case, it could be reasonable to expect the touch to go through your transparent view to the one underneath. As such, android passes touch events until they can be handled. Setting an onClickListener will handle the event, preventing it from being passed to the next View. If you wanted to be "correct" about preventing clicks on the lower fragment, there are a couple of options:

  1. Remove the onClickListener from the main fragment in OnPause, and set it in OnResume. That way, any time the main fragment is not the active fragment, it cannot be clicked.
  2. Subclass your top level layout in your VenueFragment and override OnTouchEvent to return true. This will essentially do the same thing as setting an onClickListener, but you may find that it makes your intention to block all touch events clearer.

Well the very quick answer is android:clickable="true" in the top of the layout properties in fragment.

But it would be a better practice if you create an activity with a FrameLayout & its id and no other elements in that activity. So whenever you want a fragment to be on the screen, give a call to that activity and use

getSupportFragmentManager().beginTransaction()
    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
    .replace(R.id.frameLayoutId, mainFragment, MAIN_FRAGMENT_TAG)
    .commit();

use replace instead of add so that the frameLayout of the activity is replaced by the fragment's layout as given by the frameLayoutId

I have a better solution, at least that what I think :P.

while fragment transaction, use hide and show method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top