If you want to add a custom control over your custom MapFragment, there's actually more than one way to do that. Either you can add your control in the layout of your enclosing Activity, or you can override the onCreateView method in your Fragment and add the view there.
Because the previous will add an element that is visible on top of all your Fragments displayed in your Activity (which sometimes can be useful, too), now I'm gonna go with the latter:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, parent, savedInstanceState);
TouchableWrapper touchableWrapper = new TouchableWrapper(getActivity(), this);
touchableWrapper.addView(view);
View controls = inflater.inflate(R.layout.map_overlay_controls, null);
touchableWrapper.addView(controls);
return touchableWrapper;
}
Here I'm inflating a TouchableWrapper (see Part 1 to see why) and adding a custom layout on top of that, but you could just skip those lines and call view.addView(controls) directly. In this case I'm adding a Floating Action Button from https://github.com/clans/FloatingActionButton among other things.Now the problem might not appear immediately - unless of course your mommy have taught you to always use the Don't keep activities flag in the Developer options when you were a little toddler, as she should have.
The next time your app comes back after having been put to rest by the OS and tries to restore its state, there's a good chance that it will crash and burn with a BadParcelableException:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.github.clans.fab.FloatingActionButton$ProgressSavedState
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.github.clans.fab.FloatingActionButton$ProgressSavedState
So what is the problem here and how can you solve it?