O programa é um contador, ao clicar no botão + ele adiciona +1 a variavel cont1...cont2..., Quero que quando eu clique no relatorio e eu voltar a esta pagina, os dados que eu adicionei a variavel estarem la. Sempre quando eu clico no relatorio e volto a esta pagina, reseta todos os dados. Dei uma pesquisada e devo usar o SaveInstanceState, gostaria de saber como aplicar ao meu código.
MainActivity
public class MainActivity extends AppCompatActivity {
private Button add, dim;
private TextView opt3010, opt3020, opt360, opt380, opt390, opt780, vst3268, vst3250, mon, vay;
private int cont1, cont2, cont3, cont4, cont5, cont6, cont7, cont8, cont9, cont10 = 0;
private int totalMaq = 0;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_telarelatorio:
Intent intent1 = new Intent(MainActivity.this, TelaRelatorio.class);
startActivity(intent1);
break;
case R.id.navigation_telavazia:
Intent intent2 = new Intent(MainActivity.this, TelaVazia.class);
startActivity(intent2);
break;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Menu menu = navigation.getMenu();
MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
opt3010 = (TextView) findViewById(R.id.opt3010);
opt3020 = (TextView) findViewById(R.id.opt3020);
opt360 = (TextView) findViewById(R.id.opt360);
opt380 = (TextView) findViewById(R.id.opt380);
opt390 = (TextView) findViewById(R.id.opt390);
opt780 = (TextView) findViewById(R.id.opt780);
vst3250 = (TextView) findViewById(R.id.vst3250);
vst3268 = (TextView) findViewById(R.id.vst3268);
mon = (TextView) findViewById(R.id.mon);
vay = (TextView) findViewById(R.id.vay);
add = (Button) findViewById(R.id.button);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cont1++;
opt3010.setText("" + cont1);
totalMaq++;
}
});
TelaRelatorio
public class TelaRelatorio extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent intent3 = new Intent(TelaRelatorio.this, MainActivity.class);
startActivity(intent3);
break;
}
return false;
}
};
@SuppressLint("ResourceType")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.telarelatorio);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Menu menu = navigation.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
}
}