não consigo salvar o estado da fragment quando giro ou saio dela
uma ajuda por favor estou, tentando salvar o estado de um fragment quando saio dele, já coloquei setRetainInstance(true), onSaveInstanceState , mais quando volto para fragment ou quando giro ele os valores que modifiquei não permanecem, alguem sabe outra forma
public class FragmentDebug extends Fragment {
ImageView first_country_img;
ImageView secound_country_img;
TextView first_initials;
TextView first_country_text;
TextView second_initials;
TextView secound_country_text;
EditText first_country_edit;
EditText second_country_edit;
View headerLayout;
int clicouNoPrimeiro = 0;
int clicouNoSegundo = 0;
int percentual_taxa = 0;
private Moeda moeda_primeiro_pais;
private Moeda moeda_segundo_pais;
private String flagSiglaPais1 = "brl";
private String flagSiglaPais2 = "eur";
private ICreditoBo iCreditoBo;
private IUsuarioDao iUsuarioBo;
private TextView nomeUsuario;
private TextView creditosUsuario;
private ImageView fotoUsuario;
private ProfilePictureView profilePictureView;
private SharedPreferences preferences;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conversor, null);
first_country_text = (TextView) view.findViewById(R.id.first_country_text);
secound_country_text = (TextView) view.findViewById(R.id.second_country_text);
first_initials = (TextView) view.findViewById(R.id.first_initials);
second_initials = (TextView) view.findViewById(R.id.second_initials);
first_country_img = (ImageView) view.findViewById(R.id.first_country_img);
secound_country_img = (ImageView) view.findViewById(R.id.secound_country_img);
first_country_edit = (EditText) view.findViewById(R.id.first_country_edit);
second_country_edit = (EditText) view.findViewById(R.id.second_country_edit);
view.findViewById(R.id.trocar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
trocar();
}
});
int resBandeira = getResources().getIdentifier("ic_" + flagSiglaPais1, "drawable", getActivity().getPackageName());
int resPais = getResources().getIdentifier(flagSiglaPais1, "string", getActivity().getPackageName());
first_country_text.setText(resPais);
first_initials.setText(flagSiglaPais1);
first_country_img.setImageResource(resBandeira);
first_country_edit.setText("1,0");
IMoedaBo iMoedaBo = new MoedaBo(getActivity());
Moeda m = iMoedaBo.findOne("UPPER(sigla) = UPPER(?)", new String[]{flagSiglaPais2});
ConversaoMoeda conversaoMoeda = new ConversaoMoeda(String.valueOf(m.getCotacao()), "1");
conversaoMoeda.setTaxa_itermediacao(percentual_taxa);
int resBandeira2 = getResources().getIdentifier("ic_" + flagSiglaPais2, "drawable", getActivity().getPackageName());
secound_country_text.setText(m.getPais());
second_initials.setText(m.getSigla());
secound_country_img.setImageResource(resBandeira2);
double resultado = Double.parseDouble(conversaoMoeda.retornoDeX());
second_country_edit.setText(new DecimalFormat("0.00").format(resultado));
//System.out.println(m.getSigla());
return view;
}
private String flagTrocar = "normal";private void trocar(){
String editTextFirst = first_country_edit.getText().toString();
int qtdFirst = 0;for (int i = 0; i < editTextFirst.length(); i++){
if (editTextFirst.charAt(i) == '.') {
qtdFirst++;
}
}
String first_edit = "0";
if(qtdFirst > 1) {
first_edit = first_country_edit.getText().toString().replace(".", "").replace(",",".");}else {
first_edit = first_country_edit.getText().toString().replace(",",".");
}
first_edit = (first_edit.equals("") ? "0" : first_edit);
clicouNoPrimeiro = 0;
clicouNoSegundo = 0;
Moeda moeda1 = new Moeda(first_initials.getText().toString(),
first_country_text.getText().toString(),
Double.valueOf(first_edit)
);
String editText = second_country_edit.getText().toString();
int qtd = 0;for (int i = 0; i < editText.length(); i++){
if (editText.charAt(i) == '.') {
qtd++;
}
}
String second_edit = "0";
if(qtdFirst > 1) {
second_edit = second_country_edit.getText().toString().replace(".", "").replace(",",".");}else {
second_edit = second_country_edit.getText().toString().replace(",",".");
}
second_edit = (second_edit.equals("") ? "0" : second_edit);
///Log.i("first",first_country_edit.getText().toString());
Moeda moeda2 = new Moeda(second_initials.getText().toString(),
secound_country_text.getText().toString(),
Double.valueOf(second_edit)
);
////Log.i("secound",second_country_edit.getText().toString());
secound_country_text.setText(moeda1.getPais());
second_initials.setText(moeda1.getSigla());
int resBandeira1 = getResources().getIdentifier("ic_" + moeda1.getSigla().toLowerCase(), "drawable", getActivity().getPackageName());
secound_country_img.setImageResource(resBandeira1);
second_country_edit.setText(new DecimalFormat("0.00").format(moeda1.getCotacao()));
first_country_text.setText(moeda2.getPais());
first_initials.setText(moeda2.getSigla());
int resBandeira2 = getResources().getIdentifier("ic_" + moeda2.getSigla().toLowerCase(), "drawable", getActivity().getPackageName());
first_country_img.setImageResource(resBandeira2);
first_country_edit.setText(new DecimalFormat("0.00").format((moeda2.getCotacao())));
if(flagTrocar == "normal"){
flagTrocar = "trocou";}else{
flagTrocar = "normal";
}
}
}Discussão (0)
Carregando comentários...