Save data in an Activity when changing Activity

0

The program is a counter, by clicking the + button it adds +1 to the cont1 variable ... cont2 ..., I want it when I click on the report and I go back to this page, the data I added to the variable to be there Always when I click on the report and return to this page, reset all the data. I've researched and should use SaveInstanceState, I'd 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++;
        }
    });

Reporting Screen

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);
}

}

    
asked by anonymous 12.05.2018 / 23:02

2 answers

2

As the official documentation itself says, the Activity is destroyed and recreated each time the user rotates the screen. See here: How to recreate an activity

Using onSaveInstanceState we can save the state of the Activity every time the user rotates the screen and retrieves with onRestoreInstanceState

onSaveInstanceState

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}

onRestoreInstanceState

public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);

// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

I hope I have helped. If anyone can do it any other way, please teach us. ;)

Hug.

    
14.05.2018 / 01:18
0

I think using SharePreference is a lot easier. Create a class and have it saved every time it exits, that is, save it in the onPause method, search for it, but if you need more details let me know.

    
13.05.2018 / 18:09