Android Navigation Drawer cannot set Text in fragment from MainActivity

I'm trying to put GPS coordinates in a TextView from a snippet from MainActivity, but can't figure out how to do this.

I have:

MainActivity.java LocationFragment.java 

I want to update a TextView in a LocationFragment from the MainActivity onLocationChanged() method.

Is there a chance to do this:

 public void onLocationChanged(Location location) { lat = (double) (location.getLatitude()); lng = (double) (location.getLongitude()); fragment.updateText(lat +" "+lng); } 
0
android android-activity android-fragments fragment
Jun 20 '14 at 6:44
source share
2 answers

You can use the interface for this, so the main purpose of the reuse fragment is preserved. You can implement the relationship between Activity-Fragment OR Fragment-Fragment with the following:

enter image description here

0
Jun 20 '14 at 7:45
source share

its very simple, just create one method in the fragment as shown below,

 public void SetTitle() { text_title.setText("hello fragment"); } 

Now they call this method from Activity, below is its code for <= ANDROID API 11

 HomeFragment fragment = (HomeFragment) getSupportFragmentManager() .findFragmentByTag("HomeFragment"); fragment.SetTitle(); 

for> 11 API just change getFragmentManager()

Now you can do whatever you want.

0
Jun 20 '14 at 9:32
source share



All Articles