Skip to content Skip to sidebar Skip to footer

How To Make The Number Stay In The Latest Number Instead Of Going Back To The Default?

my TxnNo(A0010001) came from Unitcode(A001) + LastTxnNo(0001). This is my button click. Db_Save.setOnClickListener(new View.OnClickListener() { @Override p

Solution 1:

Firstly, declare these two variables

SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
int count;

Next add these in onCreate method

sharedPreferences = getApplicationContext().getSharedPreferences("counters", MODE_PRIVATE);editor = sharedPreferences.edit();count = sharedPreferences.getInt("counter", 0);

Finally modify populate_TxnNo methods

privatevoid populate_TxnNo() {

        int a = count++;
        int lastNumber = Integer.valueOf(txn.getLastTxnNo());
        lastNumber = lastNumber + a;
        String stringLast = String.format("%04d", lastNumber);
        String txnNo = txn.getUnit_Code() + stringLast;
        D_Txn.setText(txnNo);
        editor.putInt("counter", ++a);
        editor.commit();
    }

Solution 2:

Try using Shared Preference

these two lines before onCreate()

 SharedPreferences prefs;
    SharedPreferences.Editor edit;  

these two lines in onCreate()

prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);edit=prefs.edit();  

and on onPause() save the string

@OverrideprotectedvoidonPause() {
    super.onPause();
    edit.putString("yourKey", txnNo);
    edit.commit();
}

and to access it back simply write in onStart

@OverrideprotectedvoidonStart() {
        super.onStart();
        String txnNo = prefs.getString("yourKey", null);
    }

Solution 3:

simply add yourNumber to shared preferences

int yourNumber;

SharedPreferencesnumber= getSharedPreferences("number", MODE_PRIVATE);

to save your number

number.edit.putInt("number",yourNumber).apply;

and read the number next time

int savedNumber = number.getInt("number",0);

Post a Comment for "How To Make The Number Stay In The Latest Number Instead Of Going Back To The Default?"