Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset AtomicInteger unique Id in Android?

I want to create a unique Id with reset button. I have a class ViewId.ViewId class contain AtomicInteger. AtomicInteger create unique Id successfully.But I want to reset unique Id using a button.So please help me to reset and create unique id again.

ViewId Class :

import java.util.concurrent.atomic.AtomicInteger;

public class ViewId {

private static ViewId INSTANCE = new ViewId();

private AtomicInteger seq;

private ViewId() {
    seq = new AtomicInteger(0);
}

public int getUniqueId() {
    return seq.incrementAndGet();
}

public static ViewId getInstance() {
    return INSTANCE;
}
}

MainActivity Class :

public class MainActivity extends AppCompatActivity {

EditText editValue;
ViewId viewId = ViewId.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editValue = (EditText)findViewById(R.id.editText);

}

// Increment Button
public void floatIncrement(View view) {
    Log.e("Incremented value is : ", String.valueOf(viewId.getUniqueId()));
}

// Reset Button
public void resetBtn(View view) {
    // reset here
}
}
like image 291
reegan29 Avatar asked Dec 24 '22 14:12

reegan29


1 Answers

Just call seq.set(0), which should just bring your AtomicInteger back to 0.

like image 162
deubaka Avatar answered Jan 10 '23 19:01

deubaka