Title: SupportMapFragment still listening after being removed (GPS is not installed)
Tags: android;google-maps;android-fragments
Question: This activity have nested fragments, one of these nested fragments is SupportMapFragment. If Google Play Services aren't installed, as fallback, it is replaced with another fragment who holds an ImageView.
An exceptions throws when I touch the ImageView, the exception is:
11-17 21:00:37.477: E/AndroidRuntime(6189): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.google.android.gms flg=0x80000 pkg=com.android.vending }
Google Play Services aren't installed, so the url 'market://' fails as expected. but:
why the click event still listening? The SupportMapFragment was removed at this point.
The relevant code parts:
public class DetailFragment extends Fragment {
///....
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
SupportMapFragment location = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mGoogleMap = location.getMap();
if (mGoogleMap != null) {
// add markers here
} else {
final View map = view.findViewById(R.id.map);
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
map.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = map.getWidth();
int height = map.getHeight();

String url = &quot;http://maps.googleapis.com/maps/api/staticmap?center=&quot; + mItem.lat + &quot;,&quot; + mItem.lon + &quot;&amp;zoom=&quot; + (int) mItem.zoom + &quot;&amp;size=&quot; + width + &quot;x&quot; + height
+ &quot;&amp;sensor=false&amp;key=API_KEY_HERE&quot;;
Bundle args = new Bundle();
args.putString(&quot;url&quot;, url);

ImageFragment img = new ImageFragment();
img.setArguments(args);
getChildFragmentManager().beginTransaction().replace(R.id.map, img).commit();
}
});
} // DetailFragmentClass
XML Relevant
<fragment
android:id=&quot;@+id/map&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:layout_marginLeft=&quot;5dp&quot;
android:layout_marginRight=&quot;10dp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:layout_weight=&quot;@dimen/promo_body_map_weight&quot;
class=&quot;com.google.android.gms.maps.SupportMapFragment&quot; /&gt;
I guess that i missing some cleanup in some point. But i can't see what.
OPTION 2:
How can i eat the click event?
As was recommended in comments, i also tried load the fragment onCreateView using a FragmentLayout in the XML but the problem still there.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_promo_datailed,
container, false);
//...
mSupportMapFragment = new SupportMapFragment();
getChildFragmentManager().beginTransaction().add(R.id.map, mSupportMapFragment).commit();
// ...
return rootView;
<FrameLayout
android:id=&quot;@+id/map&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:layout_marginLeft=&quot;5dp&quot;
android:layout_marginRight=&quot;10dp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:layout_weight=&quot;@dimen/promo_body_map_weight&quot;
/&gt;
Comment: What if you just try to set `android:clickable` to `false` at your `FrameLayout` ?

Comment: @ManishMulimani via xml layout, see edit.

Comment: @ManishMulimani The nested fragment works fine on devices with Google Play Services Installed. The issue is about replace the fragment.

Comment: "You can give a try once and let me know." OK

Comment: @ManishMulimani i tried remove android:class from fragment and add the following lines in onCreateView mSupportMapFragment = new SupportMapFragment(); getChildFragmentManager().beginTransaction().add(R.id.map, mSupportMapFragment).commit(); but get "Error Inflating class fragment"

Comment: @rom4ek I tried it but it is the same.

Comment: Did you add `SupportMapFragment` via code or xml layout file?

Comment: Nested fragments are supported only programatically. Try adding `SupportMapFragment` via code. Please check my other [answer](http://stackoverflow.com/a/26941550/2083078).

Comment: The code might work for few cases. The moment you to try to replace map fragment with image fragment, the map fragment may still be hanging around as it is not managed by child fragment manager. You can give a try once and let me know.

Comment: In your layout file, you need to replace `` with ``. You can see this [answer](http://stackoverflow.com/a/26941550/2083078).



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 10, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:11/17/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><google-maps><android-fragments>', 'Title': 'SupportMapFragment still listening after being removed (GPS is not installed)', 'ViewCount': 174, 'id': 26982162, 'user_AboutMe': "<p>Nothing in the world is a gift. Whatever there is to learn has to be learned the hard way. Turn this concepts into a viable way of life by a process of repetition. Everything new in our lives, such as the concepts we are learning, must be repeated to us to the point of exhaustion before we open ourselves to it.</p>\n<p>Said that,</p>\n<p>Beautiful is better than ugly.</p>\n<p>Explicit is better than implicit.</p>\n<p>Simple is better than complex.</p>\n<p>Complex is better than complicated.</p>\n<p>Flat is better than nested.</p>\n<p>Sparse is better than dense.</p>\n<p>Readability counts.</p>\n<p>Special cases aren't special enough to break the rules.</p>\n<p>Although practicality beats purity.</p>\n<p>Errors should never pass silently.</p>\n<p>Unless explicitly silenced.</p>\n<p>In the face of ambiguity, refuse the temptation to guess.</p>\n<p>There should be one-- and preferably only one --obvious way to do it.</p>\n<p>Although that way may not be obvious at first unless you're Dutch.</p>\n<p>Now is better than never.</p>\n<p>Although never is often better than right now.</p>\n<p>If the implementation is hard to explain, it's a bad idea.</p>\n<p>If the implementation is easy to explain, it may be a good idea.</p>\n<p>Namespaces are one honking great idea -- let's do more of those!</p>\n", 'user_AccountId': 3035018.0, 'user_DisplayName': 'rnrneverdies', 'user_Reputation': 14655.0, 'user_Views': 1172.0, 'user_id': 2573335.0}}