简体   繁体   中英

Taking data from one activity and storing it in another

I am trying to build a very simple App in Android Studio to practice using Intents to send data from one activity to another. On my Main Activity I have a "Total" TextView and a button. When I click the button, it takes me to a AddNumber Activity where I can type in an integer and press a button. This then takes me back to the Main Activity and adds that integer to the total, updating the TextView. I want to be able to do this multiple times while I've got the app open so the total keeps going up.

I have tried using a ViewModel to store the information so it doesn't get reset every time the Main Activity onCreate method is run. However, every time I do this, it works once (eg if I add a 7 in my AddNumber Activity, the Main Activity total goes to 7). However, when I try again, the ViewModel seems to get reset so doesn't remember the 7 that I put in initially, so if I put in an 8, the total on the MainActivity just gets set to 8, rather than 15. Am I making a mistake somewhere that is causing the ViewModel to reset? I understood that ViewModels were a way of storing data while the app is open, but I can't seem to make it work.

Thanks so much!

MainActivity.java code:

public class MainActivity extends AppCompatActivity {
TextView totalTextView;
MainActivityViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    totalTextView = (TextView) findViewById(R.id.totalTextView);
    viewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
    totalTextView.setText(Integer.toString(viewModel.total));
    getSetIncomingIntent();
}

public void addNumber(View view){
    Intent intent = new Intent(this, AddNumber.class);
    startActivity(intent);
}

public void getSetIncomingIntent() {
    Intent incomingIntent = getIntent();
    if (incomingIntent.hasExtra("value")) {
        viewModel.newValue = incomingIntent.getIntExtra("value",0);
        viewModel.addNumber();
        totalTextView.setText(Integer.toString(viewModel.total));
    }
}

}

MainActivityViewModel.java code:

public class MainActivityViewModel extends ViewModel {

    int total = 0;
    int newValue;
    public void addNumber() {
        total = total + newValue;
    }
}

AddNumber.java code:

public class AddNumber extends AppCompatActivity {

    EditText numberEditText;
    int value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_number);
        numberEditText = (EditText) findViewById(R.id.numberEditText);
    }

    public void addNumber(View view){
        value = Integer.parseInt(numberEditText.getText().toString());
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("value", value);
        startActivity(intent);
    }
}

When I click the button, it takes me to a AddNumber Activity where I can type in an integer and press a button. This then takes me back to the Main Activity and adds that integer to the total, updating the TextView.

This is not what is happening. First you create a new AddNumber activity.

public void addNumber(View view){
    Intent intent = new Intent(this, AddNumber.class);
    startActivity(intent); // This launches a new AddNumber Activity
}

And then here you are creating a new MainActivity instance.

public void addNumber(View view){
    value = Integer.parseInt(numberEditText.getText().toString());
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("value", value);
    startActivity(intent); // This launches a new MainActivity
}

So you're basically building a giant stack of MainActivity / AddNumber instances. You need to start AddNumber with startActivityForResult and then handle that in MainActivity . Then you will be working with the original MainActivity and the original ViewModel and you will be able to update it.

Please refer to the documentation on starting an Activity for result .

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM