如何保存最后一个片段的数据

时间:2017-09-25 09:09:37

标签: android android-fragments save fragment

所以最近我正在研究片段。到目前为止我所拥有的是一个包含三个按钮的栏(它改变了容器中的片段)。其中一个片段显示谷歌地图。但每当我更改片段时,谷歌地图的最后一个摄像头位置都没有保存。每次点击时都会初始化摄像机位置。有没有解决方案来保存我的最后一个片段数据和状态,即使我与其他片段来回走动?我搜索了诸如节目和隐藏方法之类的东西,但我认为它不起作用:(

提前谢谢!

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

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

}

2 个答案:

答案 0 :(得分:1)

每次在onClick()方法中创建片段的新实例。尝试使用这些片段创建片段的实例,而不是创建新的片段。

修改

作为一个黑客,对于你的地图片段,为你的根视图创建一个变量,并且只在onCreateView()中调用一次inflate方法:

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

答案 1 :(得分:0)

您可以选择保存和传递数据。

  1. 接口 - 从Fragment中侦听变量并将其保存为Activity并通过Intent传递数据

  2. 缓存 - 静态变量或数据存储

  3. SharedPreference或数据库