How to clear shared preferences from fragment? - android

How i can clear shared preferences from fragment? Thank you
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sharedPreferences = getActivity().getSharedPreferences("login.conf", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Intent logout = new Intent(getActivity(), LoginActivity.class);
startActivity(logout);
Log.d(TAG, sharedPreferences.getString("username", ""));
Log.d(TAG, sharedPreferences.getString("password", ""));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_logout, container, false);
}
That my fragment

you can directly use preference name and clear it from anywhere.
SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("shared_pref_key").commit();
or
SharedPreferences preferences = context.getSharedPreferences("Mypref", Context.MODE_PRIVATE);
preferences .edit().clear().commit();

Use something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment_logout, container, false);
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login.conf", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Intent logout = new Intent(getActivity(), LoginActivity.class);
startActivity(logout);
Log.d(TAG, sharedPreferences.getString("username", ""));
Log.d(TAG, sharedPreferences.getString("password", ""));
// Inflate the layout for this fragment
return view;
}

Related

Infinite loop on settheme()

I am trying to change the app theme from the fragment using setTheme() method of an activity. But somehow its looping forever while trying to change. Here is my setTheme() method calls.
//XYZ Fragment class
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
if (mThemeName.equalsIgnoreCase("Mode 1")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getActivity().setTheme(R.style.Theme_One);
} else {
getActivity().setTheme(R.style.Theme_Two);
}
super.onViewCreated(view, savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//binding object of this fragment
binding = FragmentXYZBinding.inflate(inflater);
binding.switch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if(isChecked){
setTheme("Mode 2");
}else {
setTheme("Mode 1");
}
});
return inflater.inflate(R.layout.fragment_setting, container, false);
}
public void setTheme(String name) {
SharedPreferences preferences = getActivity().getSharedPreferences("app", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("ThemeName", name);
editor.apply();
// If i call this recreate() then it goes into infinite loop
// but without calling this I can't change theme on runtime
//getActivity().recreate();
}
Any idea about what could be wrong in here ?

sharedpreferences is not saving or loading my data Android

Here is my savaData and loadData method which is used to save and load data
public void saveData(){
SharedPreferences sharedPreferences= getActivity().getSharedPreferences("SubjectTitle", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
int i=0,n;
n=addArray.size();
for(Bunk b: addArray) {
editor.putString("Title"+i, b.getTitle());
editor.putInt("No_of_bunk"+i, b.getBunk_remain());
editor.putFloat("pre_of_att"+i, b.getPrecentageAtt());
i++;
}
editor.putInt("size_of_data",n);
editor.apply();
Toast.makeText(getActivity(),"data saved",Toast.LENGTH_SHORT).show();
}
public ArrayList<Bunk> loadData(){
SharedPreferences sharedPreferences= getActivity().getSharedPreferences("SubjectTitle", Context.MODE_PRIVATE);
int n=sharedPreferences.getInt("size_pf_data",Default);
String loadTitle;
int loadBunk;
float loadAtt;
for(int i=0;i<n;i++){
loadTitle=sharedPreferences.getString("Title"+i,DEFAULT);
loadBunk=sharedPreferences.getInt("No_of_bunk"+i,Default);
loadAtt=sharedPreferences.getFloat("pre_of_att"+i,def_ault);
addArray.add(new Bunk(loadTitle,loadBunk,loadAtt));
}
return addArray;
}
here is code which is used to load data from loadData method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup perent, Bundle savedInstanceState) {
super.onCreateView(inflater, perent, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_main, perent, false);
show=(ListView)v.findViewById(android.R.id.list);
addArray=loadData();
adapter=new BunkAdapter(addArray);
show.setAdapter(adapter);
return v;
}
First, use editor.commit(); to save data after puting them, or directly: editor.putString("Title"+i, b.getTitle()).commit();
Secondly, I believe you're thinking wrong, Shared Preferences is better for things like settings or small amounts of data, in your case, you should use SQLite to save large data.

Sharedpreferences fragment

I have created two fragments. The second fragment contains multiple edittexts.
The SharedPreference works well, but only on the last edittext. For the remaining, it doesn't save anything. At the last, when we write in the edittext, then saving and running again, the app still shows the previous saved date.
EditText et;
public TwoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
et =(EditText) view.findViewById(R.id.strength_score);
et =(EditText) view.findViewById(R.id.strength_modif);
et =(EditText) view.findViewById(R.id.strength_tem_scor);
et =(EditText) view.findViewById(R.id.strength_tem_modi);
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", Context.MODE_PRIVATE);
et.setText(setting.getString("value", ""));
// Inflate the layout for this fragment
return view;
}
public void onStop( ){
super.onStop();
if(et.getText() != null) {
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", 0);
SharedPreferences.Editor editor = setting.edit();
editor.putString("value", et.getText().toString());
editor.commit();
}
}
}
Thanks for the help.
Do like this -
EditText et,et1,et2,et3;
public TwoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
et =(EditText) view.findViewById(R.id.strength_score);
et1 =(EditText) view.findViewById(R.id.strength_modif);
et2 =(EditText) view.findViewById(R.id.strength_tem_scor);
et3 =(EditText) view.findViewById(R.id.strength_tem_modi);
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", Context.MODE_PRIVATE);
et.setText(setting.getString("value", ""));
et1.setText(setting.getString("value1", ""));
et2.setText(setting.getString("value2", ""));
et3.setText(setting.getString("value3", ""));
// Inflate the layout for this fragment
return view;
}
public void onStop( ){
super.onStop();
if(et.getText() != null) {
SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", 0);
SharedPreferences.Editor editor = setting.edit();
editor.putString("value", et.getText().toString());
editor.putString("value1", et1.getText().toString());
editor.putString("value2", et2.getText().toString());
editor.putString("value3", et3.getText().toString());
editor.commit();
}
}
}

Showing error for MODE_PRIVATE within fragment in android

Showing error while using MODE_PRIVATE for sharedPreferences within onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_package_categories, container, false);
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("package_categories_id", position);
editor.commit();
return layout;
}
You should use it like this Context.MODE_PRIVATE.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_package_categories, container, false);
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("package_categories_id", position);
editor.commit();
return layout;
}
Add context in your code like :
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", Context.MODE_PRIVATE);

sending data from activity to fragment

I have a side menu, when the side menu is pressed. It should take the SharedPreferences String and send it to the Fragment. And the Fragment should set the TextView and it will be displayed on the MainActivity.
The sent data to fragment is not showing up in the MainActivity,I am getting the error. Any solutions?
Main Activity code (when option pressed):-
if(num == 0)
{
SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
FragmentOne F1 = new FragmentOne();
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
F1.setTextV(username);
FT.commit();
}
Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
return v;
}
public void setTextV(String username)
{
textV.setText(username);
}
Following issue is in current implementation :
1. If fragment_one is in layout which return in from onCreateView then use v to call findViewById :
View v = inflater.inflate(R.layout.fragment_one, container, false);
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
return v;
2. Call setTextV after FT.commit(); :
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
FT.commit();
F1.setTextV(username);
Because you are getting value from SharedPreferences to show in TextView so get value from Preferences in onCreateView and show in TextView.
In this particular case, you don't need to send data to the fragment because in a fragment you can access to SharedPreferences.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
SharedPreferences sp = getActivity().getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
textV.setText(username);
return v;
}
For other cases, the best way to send information from the Activity to the Fragment is using a Bundle.
if(num == 0)
{
SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
FragmentOne F1 = new FragmentOne();
Bundle bundle = new Bundle();
bundle.putString("username", username);
// You can add all the information that you need in the same bundle
F1.setArguments(bundle);
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
FT.commit();
}
Fragment:-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
textV.setText(getArguments().getString("username", ""));
return v;
}

Categories

Resources