0
The program is a counter, by clicking the button + it adds +1 to the variable counts1...cont2..., I want when I click on the report and I return to this page, the data I added the variable to be there. Whenever I click on the report and return to this page, reset all data. I did a search and I should use Saveinstancestate, I would like to know how to apply to my code.
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);
}
}