Keep a TextView Changed even after you close the app

1

I'm having an application for financial management, spending control for college, it's my first experience with android studio and the java language, but I'm already evolving a lot, I need to keep changing the TextView of the user's 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 gained, works perfectly, but when I recreate the solution I end up losing the balance, I would like some help to keep the value of TextView even after it is closed. follow below my Acitivity onCreate where it has the method and TextView I want to keep.   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);


} 
    
asked by anonymous 07.06.2018 / 22:04

1 answer

2

You can do this in several ways, the most viable is to create a Sqlite database, or save to a variable of type SharedPreferences, which is very used for session.  As the method of Sqlite is more complex and long you will give a researched, for a start you can go to this link: link

And here's how to use sharedPrefs:

It is declared of the second form:

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

And to redeem it, just do the following:

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