This case is for scenario when you want only 1 card:
If the Cards are not the usual 52 but still let's say you defined 3
cards, a
,b
and c
. From Deck Activity
you want to pick a card and move to CardsToChoose Activity
You can create a separate file outside the Activity called Card.kt
and store the data for the cards over there. Now, inside the Deck Activity
, you can refer to these cards with an integer
, let's say 1
for card a
, 2
for card b
and 3
for card c
. Now when user is in Deck Activity
and picks a card and wants to move to CardsToChoose Activity
, you can pass this integer
based on the Card he picked using the intent.putExtra("cardIndex", your_integer);
Now in the CardsToChoose Activity
you can get the index from Intent.
For the case where you have multiple cardIndexes
For the case where you have multiple cardIndexes to be passed, you can either pass an array
or use a ViewModel
to share the data between activities. Here is how to pass an array of Integer
Card Indices:
let's say 1 and 2 are selected card indices
int array[] = {1,2};
Intent i = new Intent(DeckActivity.this, CardsToChooseActivity.class);
i.putExtra("numbers", array);
startActivity(i);
and then you can receive the array in CardsToChooseActivity using:
Bundle extras = getIntent().getExtras();
int[] arrayB = extras.getIntArray("numbers");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…