-2

I a making a Bingo or Tambola or Housie game app...Where whenever the user clicks on generate the number button then it should get the random number and send it to activity b (or the board.java) so that in that activity(board) it will take it and highlight it so that when the users want to check the board, he could go to that activity through button and see which all numbers have been done... Here I don't know how to do it

and this is the Main activity code;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;

import android.widget.TextView;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;



public class NumberActivity extends AppCompatActivity
{
    ImageView ivBoard, ivGenerate;
    TextView tvNumber;
    public int randomNumber;
    public int count = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number);
        ivBoard = findViewById(R.id.ivBoard);
        ivGenerate = findViewById(R.id.ivGnn);
        tvNumber = findViewById(R.id.tvNumber);




        final Integer[] numbers = new Integer[90];

        for (int i = 0; i < 90; i++) {
            numbers[i] = i+1;
        }
        final Intent activityIntent;


        List<Integer> integerList = Arrays.asList(numbers);
        Collections.shuffle(integerList);
        integerList.toArray(numbers);

        randomNumber = numbers[count];
        count++;


        ivBoard.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NumberActivity.this, com.example.fullhouse.Board.class);
                startActivity(intent);
            }
        });

        ivGenerate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                randomNumber = numbers[count];
                System.out.println(randomNumber);
                count++;

                Intent intent = new Intent(NumberActivity.this, com.example.fullhouse.Board.class);
                intent.putExtra("number", randomNumber);

//                if (activityIntent != null) {
//                    startActivity(activityIntent);
//                } else {
//                    // show error that user didn't clicked on ivBoard via Toast or some logic
//                }
            }
        });

    }

}

If needed let me know to add any other code or thing I respect your answer, Thanks

Teerth jain
  • 75
  • 1
  • 1
  • 11

2 Answers2

0

As per your question, how to send data to activity b WITHOUT LAUNCHING ACTIVITY B, I'd like to say that it is possible to send data from an activity to another activity without launching it. You can use LocalBroadcastManager or EventBus.

These resources may help you a lot:

How to use LocalBroadcastManager?

EventBus in 3 steps

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
-1

You can store a whole list in the Intent object and de-serialize it:

        ivGenerate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
              Intent intent = new Intent(NumberActivity.this, 
               com.example.fullhouse.Board.class);
              List<Integer> values = getListOfIntegers();
              intent.putExtra("YOUR_KEY", values);
              startActivity(activityIntent);
            }
        });

And on the receiving side in the Board activity:

List<Integer> values = (List<Integer>) getIntent().getSerializableExtra("YOUR_KEY");
f.trajkovski
  • 794
  • 9
  • 24