Reputation: 369
I have two fragments. In the first fragment I enter text and by sharedPreferences I pass this text to the second fragment. The second fragment is displayed but without text. When debug I see that on the second fragment in the method loadText()
is null.But I don't know how to fix this error. UPD: In editor.putString("saved_text", savedApiKey);
if I enter String value (for example "hello word"
instead savedApiKey
it works good.
the first fragment:
public class FirstFragment extends Fragment {
private TextView textInFragment;
private EditText enteredApiKey;
private Button button;
private SharedPreferences sharedPreferences;
private static final String APP_PREFERENCES = "myPreferences";
private String savedApiKey;
public FirstFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
textInFragment = view.findViewById(R.id.textInFirstFragment);
enteredApiKey = view.findViewById(R.id.enteredApiKey);
button = view.findViewById(R.id.button);
savedApiKey = enteredApiKey.getText().toString();
saveText(savedApiKey);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SourceFragment newFragment = new SourceFragment();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, newFragment).addToBackStack(null).commit();
}
});
return view;
}
private void saveText(String savedApiKey) {
sharedPreferences = getContext().getSharedPreferences(APP_PREFERENCES,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("saved_text", savedApiKey);
editor.commit();
}}
and the second fragment:
public class SourceFragment extends Fragment {
private RecyclerView recyclerView;
private SharedPreferences sharedPreferences;
public static final String APP_PREFERENCES = "myPreferences";
private String API_KEY;
public SourceFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.source_fragment, container, false);
TextView text = view.findViewById(R.id.text);
text.setText(loadText());
return view;
}
private String loadText() {
sharedPreferences =getContext().getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
API_KEY = sharedPreferences.getString("saved_text", null);
return API_KEY;
}
}
Upvotes: 1
Views: 140
Reputation: 412
Use a Bundle. Here's an example:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}
and don't use Shared Preference like this. actually use the Class with name of SharedPreference and make it singleton and use it anywhere you want without any problem. sample code:
public class NameSP {
private static NameSP spInstance = null;
private Stirng Text;
public static NameSP getSPInstance(){
if (spInstance == null)
spInstance = new NameSP();
return spInstance;
}
public void saveInfo(){
SharedPreferences sp =
context.getSharedPreferences("nameSP",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.puStirng("text",yourStirng);
editor.apply();
}
public void getText(){
return this.Text;
}
}
in your fragment :
NameSP nameSp = NameSP.getInstance();
String Text = nameSP.getText();
this is just a guide , you need some methods more to load and clean and more to do. just search and keep going...
Upvotes: 1
Reputation: 141
There are no text in editText when onCreateView
calling. You should call saveText()
after your enter. For example in the button's onClickListener()
.
But better use arguments for transmission data beetween fragments.
Upvotes: 1
Reputation: 369
When I call saveText()
method in the onPause()
method like:
@Override
public void onPause() {
super.onPause();
savedApiKey = enteredApiKey.getText().toString();
saveText();
}
it works
Upvotes: 1