У меня проблема с SharedPreferences.Текст в EditText останется, хотя приложения были убиты.Но для RadioButton он не может оставаться проверенным на своем месте, когда приложения были убиты.Проверенная кнопка RadioButton останется отмеченной только в том случае, если я снова нажму кнопку «Далее» для другого действия и от другого действия до этого действия.Ниже приведен мой код:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_suggestion);
if(getIntent() != null) {
approveID = getIntent().getStringExtra("approveType");
}
else{
}
final ActionBar abar = getSupportActionBar();
View viewActionBar = getLayoutInflater().inflate(R.layout.activity_new_suggestion, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView tvTitle = viewActionBar.findViewById(R.id.title);
tvTitle.setText("NEW SUGGESTION");
abar.setCustomView(viewActionBar, params);
abar.setDisplayShowCustomEnabled(true);
abar.setDisplayShowTitleEnabled(false);
//abar.setDisplayHomeAsUpEnabled(true);
abar.setHomeButtonEnabled(true);
final SharedPreferences sharedPref = getSharedPreferences("MyData", MODE_PRIVATE);
etTitle = findViewById(R.id.etTitle);
etTitle.setText(sharedPref.getString("title", ""));
etTitle.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
sharedPref.edit().putString("title", s.toString()).apply();
}
});
etYear = findViewById(R.id.etYear);
etMonth = findViewById(R.id.etMonth);
rgSuggestWill =findViewById(R.id.rgSuggestWill);
reviewer = new ArrayList<>();
final Calendar c = Calendar.getInstance();
String mm = c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
int yy = c.get(Calendar.YEAR);
etYear.setText(new StringBuilder().append(yy));
etMonth.setText(new StringBuilder().append(mm));
spReviewer = findViewById(R.id.spReviewer);
getData();
btnNext = findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPref = getSharedPreferences("MyData",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("title",etTitle.getText().toString());
editor.putString("year",etYear.getText().toString());
editor.putString("month",etMonth.getText().toString());
int selectedId = rgSuggestWill.getCheckedRadioButtonId();
radioButton = findViewById(selectedId);
editor.putString("suggestionwill",radioButton.getText().toString());
editor.putString("reviewer",spReviewer.getSelectedItem().toString());
editor.putString("status", "Pending");
editor.apply();
Intent intent = new Intent(NewSuggestion.this, NewSuggestion2.class);
intent.putExtra("approveType",approveID);
startActivity(intent);
}
});
}
private void getData(){
//Creating a string request
User user = SharedPrefManager.getInstance(this).getUser();
StringRequest stringRequest = new StringRequest(URLs.URL_SPINNER+"?department="+user.getDepartment()+"&name="+user.getName(),
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(JSON_ARRAY);
getReviewers(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void getReviewers(JSONArray j){
for(int i=0;i<j.length();i++){
try {
JSONObject json = j.getJSONObject(i);
reviewer.add(json.getString(TAG_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
spReviewer.setAdapter(new ArrayAdapter<String>(NewSuggestion.this, android.R.layout.simple_spinner_dropdown_item, reviewer));
}
@Override
public void onBackPressed() {
SharedPreferences sharedPref = getSharedPreferences("MyData", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("title", etTitle.getText().toString());
editor.apply();
super.onBackPressed();
Intent intent = new Intent(NewSuggestion.this, DashboardApp.class);
intent.putExtra("approveType",approveID);
startActivity(intent);
}
Проблема также относится к Spinner (но не в этом классе).Кто-нибудь может помочь?