clicking on the fragment invokes activity behind it
Asked Answered
D

5

25

Im guessing its something really simple, maybe a setting in listview or fragment. But I couldnt find solution for it for couple of hours now. So.. I have a listView like this

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true">

<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fffafa"
    />

</RelativeLayout>

when someone clicks on the list item. I replace it with a fragment like this

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content, newFragment);

    ft.addToBackStack(null);
    ft.commit();

If I click on the item on the List, The whole list is replaced with a fragment(*) consisting of a Button and and couple of text fields. The Fragment replaces the listView properly with information about the item. When I press back it does properly bring the list view back.

The problem is that if i click on the background of the * it behaves as if I would click on the list that is behind it.. and brings fragment about that item to the front. I can click endlessly on background to bring other fragments. I can dig back to list by clicking back,back..

Why this fragment is transparent? how can i fix this?

Dobb answered 8/2, 2012 at 9:47 Comment(0)
D
8

I believe the issue is that the replace method doesn't replace the ListView. It only replaces other fragments that are within the container. In this specific case there are none. Thus, you're essentially adding your fragment on top of the ListView because the RelativeLayout allows views to be on top of one another. When you click on the fragment, you're not fully handling it, so it goes through to the base view which is the ListView.

A simple, quick, and dirty solution would be to retrieve the ListView object and either remove it from the container yourself or set it's visibility to GONE. A better solution would be to use ListFragment which should show the expected results.

Demonstrative answered 8/2, 2012 at 14:21 Comment(7)
i redisgned it so that list is a starting Fragment. Then overridden the obBackPressed so that it blocks taking this list away from fragment backstack if (manager.getBackStackEntryCount() == 1) and it works just fine. thanks!Dobb
I had the same problem, but with all fragments. I added my fragment in the same manner that the ListView was added: via xml. I switched to using the FragmentManager to inflate the fragment and now everything works. So the issue is that anything not added by the FragmentManager will not be removed.Indemonstrable
In case you dont have a listfragment then what should a person do?Mudpack
@Ahmed. The problem is that the View in the Fragment doesn't catch touches, so the touch goes down the View hierarchy until it finds something that does. A somewhat ugly solution would be to set an onTouchListener to the root View of a Fragment which only returns "true". This will stop all touched from going to other views when this fragment is being clicked.Demonstrative
That does the trick but when you turn on the talkback on that it would still read the screen behind. Another potential problem to deal with.Mudpack
@Ahmed. I am not familiar with tallback. The proper solution would be to properly replace the fragment with the new fragment rather than stack them on top of each other. That is stated in the original answer.Demonstrative
I tried replacing a fragment but that for me doesnt fix the issue. There may be just something else going on because of which the focus doesnt stay on the fragmentMudpack
O
29

I solved a similar problem by adding android:clickable="true" in the element that was behaving as if it was "transparent". Making it clickable made sure the click events were handled by it instead of passed to the views bellow.

Octavia answered 28/1, 2016 at 19:57 Comment(2)
problem with this is that it does not stop the haptic feedback on empty areas.Jeopardous
@SreedeviJ just add view.isHapticFeedbackEnabled = false when apply view.isClickable = trueLucic
M
13

So, I had exactly the same issue, I still don't know the reason why, but make sure your adding the android:clickable="true" to the roovView of your 2nd fragment's layout.

something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true"
android:orientation="vertical">
.....any content for your item (2nd fragment).....
</LinearLayout>
Melodrama answered 21/6, 2017 at 16:1 Comment(0)
D
8

I believe the issue is that the replace method doesn't replace the ListView. It only replaces other fragments that are within the container. In this specific case there are none. Thus, you're essentially adding your fragment on top of the ListView because the RelativeLayout allows views to be on top of one another. When you click on the fragment, you're not fully handling it, so it goes through to the base view which is the ListView.

A simple, quick, and dirty solution would be to retrieve the ListView object and either remove it from the container yourself or set it's visibility to GONE. A better solution would be to use ListFragment which should show the expected results.

Demonstrative answered 8/2, 2012 at 14:21 Comment(7)
i redisgned it so that list is a starting Fragment. Then overridden the obBackPressed so that it blocks taking this list away from fragment backstack if (manager.getBackStackEntryCount() == 1) and it works just fine. thanks!Dobb
I had the same problem, but with all fragments. I added my fragment in the same manner that the ListView was added: via xml. I switched to using the FragmentManager to inflate the fragment and now everything works. So the issue is that anything not added by the FragmentManager will not be removed.Indemonstrable
In case you dont have a listfragment then what should a person do?Mudpack
@Ahmed. The problem is that the View in the Fragment doesn't catch touches, so the touch goes down the View hierarchy until it finds something that does. A somewhat ugly solution would be to set an onTouchListener to the root View of a Fragment which only returns "true". This will stop all touched from going to other views when this fragment is being clicked.Demonstrative
That does the trick but when you turn on the talkback on that it would still read the screen behind. Another potential problem to deal with.Mudpack
@Ahmed. I am not familiar with tallback. The proper solution would be to properly replace the fragment with the new fragment rather than stack them on top of each other. That is stated in the original answer.Demonstrative
I tried replacing a fragment but that for me doesnt fix the issue. There may be just something else going on because of which the focus doesnt stay on the fragmentMudpack
U
4

Set the clickable property to true so that the click event gets consumed only on the foreground fragment.

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true" />
Unorthodox answered 21/2, 2017 at 10:16 Comment(0)
P
2

Set android:clickable="true" in FrameLayout tag of your foreground Fragment

it works for me.

Pinsk answered 6/4, 2020 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.