-1

I'm trying to change the text from a TextView that is in a Fragment when onLocationChanged function is called.

I know that i could implement LocationListener when creating HomeFragment but i want this to be modularized.

public void onLocationChanged(Location location) {

    Log.i(TAG,"onLocationChanged method called");

    HomeFragment hf = new HomeFragment();

    if(hf == null)
    {
        Log.i(TAG,"hf is null");
    }
    else {
        if(hf.getView().findViewById(R.id.speed_box) == null)
        {
            Log.i(TAG,"findViewById failed");
        }
        else {
            TextView speedBox = (TextView) hf.getView().findViewById(R.id.speed_box);

            if (location == null) {
                if (speedBox != null) {
                    speedBox.setText("unknown m/s");
                } else {
                    Log.i(TAG, "speedBox object is null");
                }
            } else {
                Log.i(TAG, "onLocationChanged method called, the speed is: " + location.getSpeed());
                float speed = location.getSpeed();

                if (speedBox != null) {
                    speedBox.setText(location.getSpeed() + " m/s");
                }
                {
                    Log.i(TAG, "speedBox object is null");
                }
            }
        }
    }
}
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44

2 Answers2

0

First of all, you probably don't want to initialize every time your Fragment class, instead of that, you should instantiate this class only once and check accessibility of this Fragment, so a few options:

  1. option - in this case, you instantiate Fragment class only once, and use this method as variable holder

    private HomeFragment hf; 
    
    public Fragment getHomeFragment() {
        if (hf == null) {
            hf = new HomeFragment();
        }
        return hf; 
    }
    
  2. Find already visible fragment:

    Fragment currentFragment = getFragmentManager().findFragmentById(R.id.fragment_container);
    if (currentFragment != null) {
        if (currentFragment instanceof HomeFragment) {
            //do your action
        }
    }
    

At least, try to post the whole class, where you have your onLocationChanged method

jantursky
  • 1,122
  • 1
  • 10
  • 21
0

You create an instance of HomeFragment, but it's not attached to the layout yet, that's why you get null from getView.

The fragment needs to be attached to Activity by a transaction from FragmentManager, then fragment.onCreateView is called, then getView will not return null.

To me, the reason you don't want to use listener is not what it should be. Location callback should be global in a location-aware app, and any component needs to listen to location changed can register a listener anywhere.

Here is how I will implement it:

  • Have a singleton AppLocationManager class that holds location logic, it keep the list of LocationListener and fire event to all listener if location changed. AppLocationManager doesn't need to know about its dependencies or what they are, it only does 1 job.
  • HomeFragment registers the listener to AppLocationManager in onCreateView, listen to the change and update its TextView.
  • Any other component can register listener to AppLocationManager if they want just like HomeFragment.
Tam Huynh
  • 2,026
  • 1
  • 16
  • 20