1

This might be a long one:

I have two fragments, one includes Google Maps and the other one is just an ordinary fragment (called InfoFragment) filled with TextViews.

On TextView I want to do next:

1. change from InfoFragment to Map fragment (via Tabs)
2. send textView ID to Map fragment
3. zoom on Marker with the same ID
    (Markers are stored in a HashMap)

I already implemented onClickListener (in InfoFragment):

linLayout.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        Toast.makeText(getContext(), "Clicked" + id, Toast.LENGTH_LONG).show();
        TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.sliding_tabs);
        tabLayout.getTabAt(0).select();
    }
});

and it is working great, tabs are switched and the right ID is stored in a variable.

Now, I want to send this ID to Map fragment, check the HashMap, chose the Marker with the same ID and zoom on it. I know how to zoom on it, but I do not know how to catch data?

If I use singleton class (which would store the ID), where in the Map fragment should I catch the data?

EDIT: If I call the method zoomOnTextViewClick():

private void zoomOnTextViewClick(final GoogleMap mMap) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this.getContext());
    textViewID = sp.getInt("text_id", -1);
    if (textViewID != -1) {
        MarkerOptions mark = hash.get(textViewID);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mark.getPosition(), 8));
    }
    //on app restart
    SharedPreferences.Editor editor = sp.edit();
    editor.putInt("text_id", -1);
    editor.apply();

In onCreateView like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.map_view, container, false);

    mView = (MapView) rootView.findViewById(R.id.mapView);
    mView.onCreate(savedInstanceState);

    mView.onResume(); // needed to get the map to display immediately

    mView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;
            //some other stuff happening also
            zoomOnTextViewClick(mMap);

        }
    });

    return rootView;
}

It will work for the first time (when I run the app)

If I call same method in onResume() like this:

@Override
public void onResume() {
    super.onResume();
    mView.onResume();

    mView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;
            zoomOnTextViewClick(mMap);

        }
    });
}

Nothing happens.

EDIT #2:

private void setTabs() {
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new FragPagerAdapter(getSupportFragmentManager(),
            MainActivity.this));

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);

    tabLayout.setupWithViewPager(viewPager);

}

This is the code which initializes my tabs.

kor
  • 41
  • 7
  • look at this https://developer.android.com/reference/android/app/Fragment.html – Umar Ata Aug 23 '16 at 20:14
  • I already have, the best thing that came to my mind was to get the data from signleton in onResume() method. But I only want to zoom on marker if there was a click on textview (if I use onResume(), it will try to zoom on marker everytime I change tabs) – kor Aug 23 '16 at 20:24
  • you need to declare a variable and store its state in shared preferences and set it if the zoom is performed – Umar Ata Aug 23 '16 at 20:26
  • Do you mean, set it, if the click on TextView is performed? Still doesn't answer my question, yes, the variable will be stored, but I don't know how to perform zoom after tabs are switched. – kor Aug 23 '16 at 20:29
  • when you came back to map fragment get the value of variable and perform the zoom programatically, you may do it by using function performClick(); or you may google it for more detailed answer – Umar Ata Aug 23 '16 at 20:32
  • and in your question you already mentioned that you know how to zoom but not know how to catch data, I told you the way to catch data – Umar Ata Aug 23 '16 at 20:33
  • What I got from your answer is how to store the variable. Thank you for that, would not think of that option. However, I still do not know WHERE and WHEN in Map fragment should I use the data from preferences and make the zoom? I want to do it right after tabs are switched. – kor Aug 23 '16 at 20:40
  • do it oncreateview method just before returning the view – Umar Ata Aug 23 '16 at 20:41
  • Hmmm I have to put it at the end of onMapReady() method and it doesn't appear to work so far. – kor Aug 23 '16 at 21:24
  • does your problem is solved? – Umar Ata Aug 23 '16 at 21:30
  • Not yet.. I have 2 problems. One is, I need to reset the int which is stored into shared preferences - after app reset it zooms to the ID saved (I will probably overwrite the int to -1 after zooming in). second is.. My method for zooming in onCreateView is called only once - in the beginning, but nothing is happening afer tab change. – kor Aug 23 '16 at 21:41
  • definitely you need to reset the variable and you can use onstart or on resume method to call your zoom function everytime – Umar Ata Aug 23 '16 at 21:44
  • and now if you face any error then post the code you used and also the logcat error detail – Umar Ata Aug 23 '16 at 21:45
  • Somehow neither is working. Will have to look into those methods a bit more. The point is, everything is working perfectly I just cant get my app to call the zoom method – kor Aug 23 '16 at 21:52
  • post the you tried to call it and where you called it – Umar Ata Aug 24 '16 at 03:43
  • Please, see my edited quesion. – kor Aug 24 '16 at 13:05
  • post the code on whci you are changing the fragment – Umar Ata Aug 24 '16 at 13:13
  • What do you mean by changing the fragmet? Where I switch the tabs orwhat? – kor Aug 24 '16 at 13:16
  • are you using a tabhost with fragment or viewpager – Umar Ata Aug 24 '16 at 13:18
  • I'm using tabLayout. In my question, I already pasted the code which shows how I change between tabs. With viewPager – kor Aug 24 '16 at 13:21
  • now put a breakpoint on every method of fragment andcheck which one is called first on debugging – Umar Ata Aug 24 '16 at 13:27
  • Which one should be called first? – kor Aug 24 '16 at 13:39
  • check this for that https://developer.android.com/guide/components/fragments.html – Umar Ata Aug 24 '16 at 13:43
  • Okay I know what is the problem. Fragment is not 'resumed' after the tab switch. I'll try to implement this: http://stackoverflow.com/questions/6503189/fragments-onresume-from-back-stack dunno how, but will try – kor Aug 24 '16 at 13:56

0 Answers0