从Fragment到Activity的Android SharedPreferences

时间:2017-01-05 16:08:09

标签: android sharedpreferences

我有一个LoginFragment()课程和ProfileFragment()课程,已在NavigationDrawerActivity中实施。

登录LoginFragment()后,NavigationDrawerActivity即已启动。 当我按导航抽屉活动中的设置时,有ProfileFragment,我可以在其中更改用户密码或注销。

我在ProfileFragment LoginFragment中从SharedPreferences下载用户数据时遇到问题。

import android.app.Fragment; import android.app.FragmentTransaction; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.design.widget.Snackbar; import android.support.v7.widget.AppCompatButton; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class LoginFragment extends Fragment implements View.OnClickListener { private AppCompatButton btn_login; private EditText et_email,et_password; private TextView tv_register; private ProgressBar progress; private SharedPreferences pref; private SharedPreferences.Editor editor; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_login,container,false); initViews(view); return view; } private void initViews(View view){ pref = getActivity().getPreferences(0); btn_login = (AppCompatButton)view.findViewById(R.id.btn_login); tv_register = (TextView)view.findViewById(R.id.tv_register); et_email = (EditText)view.findViewById(R.id.et_email); et_password = (EditText)view.findViewById(R.id.et_password); progress = (ProgressBar)view.findViewById(R.id.progress); btn_login.setOnClickListener(this); tv_register.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_register: goToRegister(); break; case R.id.btn_login: String email = et_email.getText().toString(); String password = et_password.getText().toString(); if(!email.isEmpty() && !password.isEmpty()) { progress.setVisibility(View.VISIBLE); loginProcess(email, password); } else { Snackbar.make(getView(), "Uzupełnij pola!", Snackbar.LENGTH_LONG).show(); } break; } } private void loginProcess(String email,String password){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); RequestInterface requestInterface = retrofit.create(RequestInterface.class); User user = new User(); user.setEmail(email); user.setPassword(password); ServerRequest request = new ServerRequest(); request.setOperation(Constants.LOGIN_OPERATION); request.setUser(user); Call<ServerResponse> response = requestInterface.operation(request); response.enqueue(new Callback<ServerResponse>() { @Override public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) { ServerResponse resp = response.body(); Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show(); if(resp.getResult().equals(Constants.SUCCESS)){ editor = pref.edit(); editor.putBoolean(Constants.IS_LOGGED_IN,true); editor.putString(Constants.EMAIL,resp.getUser().getEmail()); editor.putString(Constants.NAME,resp.getUser().getName()); editor.putString(Constants.UNIQUE_ID,resp.getUser().getUnique_id()); editor.apply(); goToProfile(); } progress.setVisibility(View.INVISIBLE); } @Override public void onFailure(Call<ServerResponse> call, Throwable t) { progress.setVisibility(View.INVISIBLE); Log.d(Constants.TAG,"failed"); Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show(); } }); } private void goToRegister(){ Fragment register = new RegisterFragment(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.fragment_frame,register); ft.commit(); } private void goToProfile(){ // Fragment profile = new ProfileFragment(); // FragmentTransaction ft = getFragmentManager().beginTransaction(); // ft.replace(R.id.fragment_frame,profile); // ft.commit(); startActivity(new Intent(getActivity(), MainActivity.class)); } } 编辑器,但它不包含我的USER设置。

我在LoginFragment()中的日志记录过程代码:

    @Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    pref = LoginFragment().pref;
    //pref = getActivity().getPreferences(0);
    SharedPreferences.Editor editor = LoginFragment().editor;
    tv_name.setText("Welcome : "+pref.getString(Constants.NAME,""));
    tv_email.setText(pref.getString(Constants.EMAIL,""));

}

一些ProfileFragment()类:

private void logout() {
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean(Constants.IS_LOGGED_IN,false);
    editor.putString(Constants.EMAIL,"");
    editor.putString(Constants.NAME,"");
    editor.putString(Constants.UNIQUE_ID,"");
    editor.apply();
    startActivity(new Intent(getContext(), LoginActivity.class));
}

SharedPreferences

如何解决它将一个 ProfileFragment galleryFragment = new ProfileFragment(); getSupportFragmentManager().beginTransaction() .setCustomAnimations(R.anim.enter,R.anim.exit,R.anim.pop_enter, R.anim.pop_exit) .replace(R.id.content_frame, galleryFragment) .addToBackStack(null) .commit(); 与另一个进行整合? 提前谢谢!

编辑://我从MainActivity()(其导航抽屉)启动一个ProfileFragment()类:

sudo sshuttle --dns -vr user@172.16.30.30 0/0

1 个答案:

答案 0 :(得分:0)

而不是getActivity().getPreferences(0);在您的活动中使用PreferenceManager.getDefaultSharedPreferences(this);或在您的片段中使用PreferenceManager.getDefaultSharedPreferences(getContext());