如何在saveInstanceState中保存数据并重新加载数据

时间:2018-10-11 12:23:32

标签: java android

我想保存更改后的数据片段。我有这样的片段:

public class panel_fragment extends Fragment {
SharedPreferences preferences;
LinearLayout variz, tasviye, team;
FragmentTransaction transaction;
ImageButton exit;
SharedPreferences.Editor editor;
TextView name, subset_id, phone, numTeam, daramad, loadTasviye;
profile data;
ProgressBar bar;
ConstraintLayout layout_profile;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, container, false);


    SetUp(view);
    getProfile(view);
    return view;
}
void getProfile(final View view){
    my_api api= ApiClinet.getRetrofit().create(my_api.class);
    Call<profile> call = api.getProfile(preferences.getString("phone",""));
    call.enqueue(new Callback<profile>() {
        @Override
        public void onResponse(Call<profile> call, Response<profile> response) {
            data=response.body();
            set_data(view,data);


        }

        @Override
        public void onFailure(Call<profile> call, Throwable t) {

        }
    });

}

我获得了带有改装和设置数据的课程资料

我想将数据保存在savedInstanceState中,然后尝试

  @Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelable("profile", (Parcelable) data);
}

并获得

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, container, false);
    Set_Up(view);
    if (savedInstanceState != null) {
        data = (profile) savedInstanceState.get("profile");
        set_data(view, data);
    } else {


        getProfile(view);
    }
    return view;
}

我有3个片段,并在更换时替换。

不起作用。请帮助我。

0 个答案:

没有答案