inazaruk / map-fragment Goto Github PK
View Code? Open in Web Editor NEWAn example of how one can use MapActivity as a fragment.
An example of how one can use MapActivity as a fragment.
Hello, I'm trying to run your example project, but both mapviews is empty with this errors in logcat:
IOException processing: 26
java.io.IOException: Server returned: 3
at android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData(BaseTileRequest.java:115)
at android_maps_conflict_avoidance.com.google.googlenav.map.MapService$MapTileRequest.readResponseData(MapService.java:1473)
at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.processDataRequest(DataRequestDispatcher.java:1117)
at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.serviceRequests(DataRequestDispatcher.java:994)
at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher$DispatcherServer.run(DataRequestDispatcher.java:1702)
at java.lang.Thread.run(Thread.java:856)
Sorry I'm new to android, thank you so much for the code.
I have an activity and fragments in it. I use the code below to change the fragments (where f is the new fragment)
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, f);
ft.addToBackStack(null);
ft.commit();
I tried to f = new LocalActivityManagerFragment(); but it doesn't work....
How can I change between my fragments and the example map fragment?
Thank you so much!!!!
And also, it seems it's not necessary to have my_map_fragment.xml (the one with tab host) ? Is that true?
Sorry,
I am new in android but I have this problem when I tried to implement your solution. I always get this errors and I cant google any right solutions.
11-24 01:46:29.183: ERROR/AndroidRuntime(379): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sito/com.sito.map.GoogleMapActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at com.sito.map.ActivityHostFragment.onCreateView(ActivityHostFragment.java:22)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at com.sito.MainActivity$TabManager.onTabChanged(MainActivity.java:189)
at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359)
at android.widget.TabHost.setCurrentTab(TabHost.java:344)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1657)
at com.sito.map.GoogleMapActivity.onCreate(GoogleMapActivity.java:25)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
... 26 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 36 more
Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:397)
at com.google.android.maps.MapView.(MapView.java:289)
at com.google.android.maps.MapView.(MapView.java:264)
at com.google.android.maps.MapView.(MapView.java:247)
... 39 more
Hi Inazaruk!
Based on your code I combined the two Fragment classes into one and based it off my own custom Fragment:
public abstract class ActivityHostFragment<T> extends ContentFragment<T> {
private static final String KEY_STATE_BUNDLE = "localActivityManagerState";
private static final String ACTIVITY_TAG = "hosted";
private View activityView;
private LocalActivityManager localActivityManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle state = null;
if (savedInstanceState != null) {
state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
}
localActivityManager = new LocalActivityManager(getActivity(), true);
localActivityManager.dispatchCreate(state);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
View fragmentView = super.onCreateView(inflater, container, state);
ViewGroup containerView = getContainerView(fragmentView);
activityView = startActivityAndGetView();
containerView.addView(activityView);
return fragmentView;
}
@Override
public void onDestroyView() {
ViewGroup containerView = getContainerView(getView());
containerView.removeAllViews();
activityView = null;
super.onDestroyView();
}
private ViewGroup getContainerView(View fragmentView) {
int containerId = getActivityContainerView();
View containerView = fragmentView.findViewById(containerId);
checkArgument(containerView != null, "container with ID " + containerId + " not found");
checkArgument(containerView instanceof ViewGroup, "container with ID " + containerId + " is not a viewGroup");
return (ViewGroup) containerView;
}
protected View startActivityAndGetView() {
Intent intent = new Intent(getActivity(), getActivityClass());
Window window = localActivityManager.startActivity(ACTIVITY_TAG, intent);
View view = window != null ? window.getDecorView() : null;
if (view != null) {
ViewParent parent = view.getParent();
if (parent != null) {
ViewGroup v = (ViewGroup) parent;
v.removeView(view);
}
view.setVisibility(View.VISIBLE);
view.setFocusableInTouchMode(true);
if (view instanceof ViewGroup) {
((ViewGroup) view).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
}
}
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(KEY_STATE_BUNDLE, localActivityManager.saveInstanceState());
}
@Override
public void onResume() {
super.onResume();
localActivityManager.dispatchResume();
}
@Override
public void onPause() {
super.onPause();
localActivityManager.dispatchPause(getActivity().isFinishing());
}
@Override
public void onStop() {
super.onStop();
localActivityManager.dispatchStop();
}
@Override
public void onDestroy() {
super.onDestroy();
localActivityManager.dispatchDestroy(getActivity().isFinishing());
}
/**
* Returns the view instance that has been embedded into this fragment's
* view
*
* @return
*/
public View getActivityView() {
return activityView;
}
/**
* Returns the class of the activity that should be "hosted" in this
* fragment
*
* @return
*/
protected abstract Class<? extends Activity> getActivityClass();
/**
* Returns the ID of the container that is part of the layout defined in
* {@#getLayout()} in which the activity view should be
* loaded into
*
* @return
*/
protected abstract int getActivityContainerView();
}
This works all fine as is, just that I encounter two major problems:
StrictMode
tells me that I'm leaking my MapActivity that I return as getActivityClass()
in a derived class; what is weird about that is that I could only reproduce it to leak up to 3 or 4 instances of the activity, so it seems to be cleaned, but at least two instances still remain (verified with HPROF dump in MAT)BACK
button does not seem to work properly any longer. It takes sometimes up to five clicks on it to actually make it call my onBackPressed()
handler in my main activity. I get many log messages from the InputEventConsistencyVerifier
telling me about ACTION_UP but key was not down
, originating from android.widget.ZoomButtonsController
Have you experienced anything like this in the past?
I know that there is a new API finally available, but I read of problems with it on older devices (< 3.0), so we decided to stick to the v1 API for now.
Thanks in advance!
Thomas.
now that LocalActivityManager is deprecated in API 13, can someone update this example using FragmentManager?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.