1

So recently I'm working on fragments. and what I have so far is a bar that contains three buttons(which changes the fragment in the container). and one of the fragments shows Google Map. But whenever I change the fragment, the last camera location of Google Map wasn't saved. And the camera position is initialized every time when I click. Are there any solution to save my last fragment data and status even if I go back and forth with other fragments? I've searched things like show and hide methods but I don't think it's working :(

Thx in advance!

private Toolbar toolbar;
ImageButton btn_googleMap;
ImageButton btn_localTimeLine;
ImageButton btn_timeLine;
ImageButton btn_myProfile;
Fragment_googlemap fragment_googlemap = new Fragment_googlemap();
Fragment_localtimeline fragment_localtimeline = new Fragment_localtimeline();
Fragment_timeline fragment_timeline = new Fragment_timeline();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    btn_localTimeLine = (ImageButton) findViewById(R.id.btn_localTimeLine);
    btn_timeLine = (ImageButton) findViewById(R.id.btn_timeLine);
    btn_googleMap = (ImageButton) findViewById(R.id.btn_mapTrending);
    btn_myProfile = (ImageButton)findViewById(R.id.imgbtn_myProfile);

    // Initialized fragment for Google Map
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.add(R.id.container,new Fragment_googlemap());
    ft.commit();

    // Click listener for other fragments
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment = null;
            if (v == findViewById(R.id.btn_mapTrending)) {
                fragment = fragment_googlemap;
            } else if (v == findViewById(R.id.btn_localTimeLine)) {
                fragment = fragment_localtimeline;
            } else if (v == findViewById(R.id.btn_timeLine)) {
                fragment = fragment_timeline;
            }

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.replace(R.id.container, fragment);
            ft.commit();
        }
    };

    btn_googleMap.setOnClickListener(listener);
    btn_localTimeLine.setOnClickListener(listener);
    btn_timeLine.setOnClickListener(listener);

here's a Fragment_googlemap!

public class Fragment_googlemap extends android.support.v4.app.Fragment 
implements  OnMapReadyCallback{

View mRootView;
private MapView mapView;
private Spinner spinner_countries;
private GoogleMap googleMap;
FloatingActionButton floatingActionButton;

public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if(mRootView==null){
        // Inflate the layout for this fragment
        mRootView= inflater.inflate(R.layout.fragment_googlemap, container, false);
    }
    return  mRootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mapView = (MapView) view.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    mapView.onResume();
    mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment


    spinner_countries = (Spinner)view.findViewById(R.id.spinner_countries);
    floatingActionButton = (FloatingActionButton)view.findViewById(R.id.fab);
    floatingActionButton.setElevation(100);

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

@Override
public void onMapReady(GoogleMap map) {
    googleMap = map;
}

}

Seungho Lee
  • 1,068
  • 4
  • 16
  • 42

2 Answers2

1

You are creating a new instance of the fragment every time in the onClick() method. Try creating instances of the fragments at the class level using those instead of creating new ones.

Edit:

As a hack, for your maps fragment, create a variable for your root view and only call the inflate method in your onCreateView() once:

class Fragment_googlemap {
    ...
    View mRootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_googlemap, container, false);
        }

        return mRootView;
    }
}
kRiZ
  • 2,320
  • 4
  • 28
  • 39
  • I've changed codes like above as you adviced but it still doesn't save the last state ; _ ; – Seungho Lee Sep 25 '17 at 09:23
  • Thanks for your advice, but since I'm new to android, I don't know what to touch from my code :( If you have a bit of time , could you please take a simple look at my Fragment_googlemap also? – Seungho Lee Sep 25 '17 at 09:37
  • Your fragment class looks okay. The `onCreateView()` class is called when the fragment comes on screen which causes the layout to be "inflated" (created on the screen) every time as well. Following my above edit, this line of code is wrapped in an `if` statement so that it only gets called once. – kRiZ Sep 25 '17 at 09:43
  • I've changed the codes as you told me like above, but it's won't work :( but, thank you so much for helping me out! and It would be great to inform me anything if you have an any better idea!! – Seungho Lee Sep 25 '17 at 10:07
  • From your edit, I see that the `mRootView` variable is in your activity and not the fragment class. I assume that that's just a typo? – kRiZ Sep 25 '17 at 10:08
  • oops my bad!! it's just a typo sorry if it confused you : ) and I've done your suggestion perfectly i assume : ) – Seungho Lee Sep 25 '17 at 10:11
  • My last suggestion would be to use `show` and `hide` instead or replace for your fragments. See the answer in this post: https://stackoverflow.com/questions/14114383/saving-mapfragment-maps-v2-state-in-android – kRiZ Sep 25 '17 at 10:16
0

You have some options to save and pass your data.

  1. Interface - Listening for variable changes from Fragment and Saving it to Activity and Passing data through Intent

  2. Cache - Static Variable or Data Store

  3. SharedPreference or Database

kimkevin
  • 2,202
  • 1
  • 26
  • 48