【问题标题】:How to save the data of last fragment如何保存最后一个片段的数据
【发布时间】:2017-09-25 09:09:37
【问题描述】:

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

提前谢谢!

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

}

【问题讨论】:

    标签: android android-fragments save fragment


    【解决方案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;
        }
    }
    

    【讨论】:

    • 我已经按照您的建议更改了上面的代码,但它仍然没有保存最后一个状态; _ ;
    • 感谢您的建议,但由于我是 android 新手,我不知道从我的代码中触摸什么 :( 如果您有一点时间,请您简单看一下也在我的 Fragment_googlemap 上?
    • 您的片段类看起来不错。当片段出现在屏幕上时会调用onCreateView() 类,这会导致布局每次都“膨胀”(在屏幕上创建)。在我上面的编辑之后,这行代码包含在 if 语句中,因此它只被调用一次。
    • 我已经按照你上面告诉我的那样更改了代码,但它不起作用:(但是,非常感谢你帮助我!如果有任何事情,我会很高兴告诉我你有更好的主意!!
    • 从您的编辑中,我看到 mRootView 变量在您的活动中而不是片段类中。我认为这只是一个错字?
    【解决方案2】:

    您有一些选项来保存和传递您的数据。

    1. 接口 - 监听来自 Fragment 的变量变化并将其保存到 Activity 并通过 Intent 传递数据

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

    3. SharedPreference 或数据库

    【讨论】:

    • 实际上我正在使用数据存储进行缓存。链接是github.com/kimkevin/CachePot。谢谢!
    • 哦,存储相机位置是否也适用于您的想法?顺便说一句,感谢您纠正我糟糕的语法:)
    • @poream3387 是的!我认为您需要为具有要保存的数据(例如位置或地图信息)的结果创建类。
    • 谢谢!我会尝试你的建议:)
    猜你喜欢
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多