Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to notify Activity when Fragments views are ready?

I'm using Google Maps API V2 in an activity wih drop down navigation where the map is on the second position.

I'm adding the map pragmatically like:

mMapFragment = supportMapFragment.newInstance(); getSupportFragmentManager()         .beginTransaction()         .replace(R.id.placeHolder, mMapFragment, TAG_MAP)         .commit();  

I want to get the GoogleMap ovject, as the documentation https://developers.google.com/maps/documentation/android/map say it should be done with mMapFragment.getMap(), but it returns null.

according to http://developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html it returns null if the Fragment hasn't gone through the onCreateView lifecycle event.

how can I know whenever the fragment is ready ?

EDIT: I found this How do I know the map is ready to get used when using the SupportMapFragment?

Overriding onActivityCreated seems like a solution, but then I'll have to instantiate the fragment through a constructor and not with newInstance(), does it make any difference ?

like image 797
Gal Ben-Haim Avatar asked Feb 21 '13 10:02

Gal Ben-Haim


People also ask

How do you know when a fragment becomes visible?

fragment:fragment:1.1. 0 you can just use onPause() and onResume() to determine which fragment is currently visible for the user. onResume() is called when the fragment became visible and onPause when it stops to be visible. To enable this behavior in the first ViewPager you have to pass FragmentPagerAdapter.

Can we call activity from fragment in Android?

Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick() method in that interface. Now implement it to your first activity and call second activity from there.

Does fragment have onCreate?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.


1 Answers

My preferred method is to use a callback to get a signal from a Fragment. Also, this is the recommended method proposed by Android at Communicating with the Activity

For your example, in your Fragment, add an interface and register it.

public static interface OnCompleteListener {     public abstract void onComplete(); }  private OnCompleteListener mListener;  public void onAttach(Context context) {     super.onAttach(context);     try {         this.mListener = (OnCompleteListener)context;     }     catch (final ClassCastException e) {         throw new ClassCastException(context.toString() + " must implement OnCompleteListener");     } } 

Now implement this interface in your Activity

public class MyActivity extends FragmentActivity implements MyFragment.OnCompleteListener {     //...      public void onComplete() {         // After the fragment completes, it calls this callback.         // setup the rest of your layout now         mMapFragment.getMap()     } } 

Now, whatever in your Fragment signifies that it's loaded, notify your Activity that it's ready.

@Override protected void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     // create your fragment     //...      // signal that you're done and tell the Actvity to call getMap()     mListener.onComplete(); } 

EDIT 2017-12-05 onAttach(Activity activity) is deprecated, use onAttach(Context context) instead. Code above adjusted.

like image 64
Kirk Avatar answered Oct 19 '22 19:10

Kirk