How to compare data from different activities?

Asked

Viewed 83 times

1

I’m developing a simple app just to increase my knowledge, and I want to know how I can compare data between activities?

Example:

Activity1: In this Activity I have the variable A which has as value 1

Activity2: In this other one, I have the variable B which has as value 2

Activity3: And in this a variable C with the value 3

Now what I want to know, at Activity 0 when I enter the name of the variable A for example, it shows me its value and at the same time tell me what the lowest value is between the other variables of the others activities!

  • It would be important for you to look at the Activity cycle of android and analyze which one you need to use. I suggest reading: https://developer.android.com/guide/components/activities.html?hl=pt-br

1 answer

4


In this case you have 4 options in my view:

1 - You will have to traffic the information of a view to another through intents and in that you read the value with getExtras() of Intent in the other Activity, as in the example below:

Intent intent = new Intent(this, Activity2.class)
intent.putExtra("var1", 10);
startActivity(intent);

//Aqui você vai ler na Activity2
getIntent().getIntExtra("var1", 0) //0 é default se não achar um extra com a key var1

2 - Or save a static class that will store the values for you to compare;

3 - Or save these values in any form of data persistence, either in Sharedpreferences or Database;

4 - Or in the way I find simple to example, having a Activity and the other three being Fragments.

  • Now I understand, I think via Database would be simpler and would complicate my life less later, but thanks for the answer.

Browser other questions tagged

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