Save data to an Activity when changing Activity

Asked

Viewed 305 times

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

}

inserir a descrição da imagem aqui

2 answers

2

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

Using the onSaveInstanceState we can save the state of Activity every time the user rotates the screen and recover with the 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 helped. If anyone can do otherwise, please teach us. ;)

Hug.

0

I believe that using Sharepreference is much easier. Create a class and have all the data saved whenever you leave, that is, save in the method onPause, search on, but if you need more details let me know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.