Seungho Lee
Seungho Lee

Reputation: 1155

How to save the data of last fragment

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;
}

}

Upvotes: 1

Views: 477

Answers (2)

kimkevin
kimkevin

Reputation: 2222

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

Upvotes: 0

kRiZ
kRiZ

Reputation: 2316

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;
    }
}

Upvotes: 1

Related Questions