Keep a Changed Textview even after closing the app

Asked

Viewed 118 times

1

Guys I’m making an application for financial management, spending control for college, is my first experience with android studio and the java language, but I’m already evolving enough, I need to keep changed Textview user balance, the same in Oncreate has a Method that brings from another Activity a value to be treated according to what the user informs in the case spent or won, works perfectly, but when recreates the solution I end up losing the balance, I would like help to keep the value of Textview even after it is closed. follows down the onCreate of my Acitivity where has the method and the Textview that I wish to maintain. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); Toolbar Toolbar = (Toolbar) findViewById(R.id.Toolbar); txtSaldo=(TextView)findViewById(R.id.txtSaldo); setSupportActionBar(Toolbar); Intent Intent = getIntent(); Bundle Bundle = Intent.getExtras(); if(Bundle!=null) { String txt = Bundle.getString("txt"); String test=Bundle.getString("test");

           if(teste!=null) {
               if (teste.contains("+")) {
                   v2 = txt;
                   double vb = Double.parseDouble(v2);
                   v1 = txtSaldo.getText().toString();
                   double va = Double.parseDouble(v1);
                   double total = vb + va;
                   resultado = Double.toString(total);

                   txtSaldo.setText(resultado);

               } else {
                   v2 = txt;
                   double vb = Double.parseDouble(v2);
                   v1 = txtSaldo.getText().toString();
                   double va = Double.parseDouble(v1);
                   double total = va - vb;
                   resultado = Double.toString(total);
                   txtSaldo.setText(resultado);
               }
           }


    }


    smartTabLayout = findViewById(R.id.smartTabLayout);
    viewPager = findViewById(R.id.viewpager);
    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
            getSupportFragmentManager(),
            FragmentPagerItems.with(this)
                    .add("Últimos lançamentos", ListaFragment.class)
                    .add("Histórico Mês", HistoricoFragment.class)
                    .add("Gráficos", GraficoFragment.class)
                    .create()
    );
    viewPager.setAdapter(adapter);
    smartTabLayout.setViewPager(viewPager);


} 
  • 1

    You need to save this information to a database and retrieve it when starting the app. Search for data persistence on Android.

1 answer

2


You can do this in various ways, the most viable is to create a Sqlite database, or save it in a Sharedpreferences type variable, which is widely used for session. As the Sqlite method is more complex and long you will do a search, to start can go on this link: https://www.devmedia.com.br/criando-um-crud-com-android-studio-e-sqlite/32815

And here I will demonstrate how to use sharedPrefs:

It is stated in the second way:

SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("VALOR_TXT", seuedittext.getText().toString());
editor.commit();

And to rescue her just do the following:

SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
String algumaString = pref.getString("VALOR_TXT", "");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.