Main Activity not updating intent returns

39 Views Asked by At

I'm developing a game where my main activity (GameActivity) is hide-and-seek. Then there's a second activity (InventoryActivity) where players can use items that affect their characteristics (number of hit points, for example). I'm able to retrieve the current player's hit points in the second activity, and modify them if he uses an item from his inventory. The problem comes when I finish the 2nd activity: the hit points are not sent to the main activity.

I think it's a problem of intent not being properly returned to the main activity. To retrieve the information for my second activity, I did the following in my main activity:

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1001 && resultCode == RESULT_OK && data != null) {
            ArrayList<Item> itemsFound = data.getParcelableArrayListExtra("ITEMS_FOUND");
            ArrayList<Item> playerInventory = data.getParcelableArrayListExtra("PLAYER_INVENTORY");
            int updatedHearts = data.getIntExtra("CURRENT_PLAYER_HEARTS", 0); 
            currentPlayerIndex = data.getIntExtra("CURRENT_PLAYER_INDEX", 0); 
            Player modifiedPlayer = data.getParcelableExtra("CURRENT_PLAYER"); 
    
    
            if (modifiedPlayer != null) {
                for (int i = 0; i < allPlayers.size(); i++) {
                    Player currentPlayer = allPlayers.get(i);
                    if (currentPlayer.getName().equals(modifiedPlayer.getName())) {
                       
                        allPlayers.set(i, modifiedPlayer);
                       
                        updateHeartsDisplay(modifiedPlayer.getHearts());
                       
                        Log.d("GameActivity", "Joueur mis à jour : " + modifiedPlayer.getName() + ", Points de vie : " + modifiedPlayer.getHearts());
                        break;
                    }
                }
            }
        }
    } ```
To launch the inventory activity using intents, I used the following:


```private void launchInventoryActivity(Player currentPlayer) {     // Prepare data for inventory activity     ArrayList<Item> itemsFound = new ArrayList<>();     Item lastItemAdded = currentPlayer.getLastItemAdded();     if (lastItemAdded != null) {         itemsFound.add(lastItemAdded);     }     ArrayList<Item> playerInventory = new ArrayList<>();     playerInventory.addAll(currentPlayer.getItems());      Intent inventoryIntent = new Intent(GameActivity.this, InventoryActivity.class);     inventoryIntent.putParcelableArrayListExtra("ITEMS_FOUND", itemsFound);     inventoryIntent.putParcelableArrayListExtra("PLAYER_INVENTORY", playerInventory);     inventoryIntent.putExtra("CURRENT_PLAYER", currentPlayer);        inventoryLauncher.launch(inventoryIntent); }```  


Finally, in my secondary activity, I return the player's information ( with his modified hit points ) to my main activity with this;

```private void finishTurn() {
        int currentPlayerIndex = getIntent().getIntExtra("CURRENT_PLAYER_INDEX", 0);
        Intent intent = new Intent();
        intent.putExtra("CURRENT_PLAYER_HEARTS", currentPlayer.getHearts());
        intent.putExtra("CURRENT_PLAYER_INDEX", currentPlayerIndex + 1);
        intent.putExtra("CURRENT_PLAYER", currentPlayer);
        setResult(RESULT_OK, intent);
        finish();
    }

Thank you for your help. I saw the new method because of the deprecated api and i did this, but i still have the same problem.


            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == RESULT_OK) {
                        // Vérifiez si les données de point de vie sont renvoyées
                        Intent data = result.getData();
                        if (data != null) {
                            int currentPlayerHealth = data.getIntExtra("CURRENT_PLAYER_HEARTS", -1);
                            if (currentPlayerHealth != -1) {
                              
                                Player currentPlayer = data.getParcelableExtra("CURRENT_PLAYER");
                                if (currentPlayer != null) {
                                    currentPlayer.setHearts(currentPlayerHealth);
                                   
                                    updateTurnInfo();
                                }```

this is my private void launchInventoryActivity method now :

```private void launchInventoryActivity(Player currentPlayer) {
    ArrayList<Item> itemsFound = new ArrayList<>();
    Item lastItemAdded = currentPlayer.getLastItemAdded();
    if (lastItemAdded != null) {
        itemsFound.add(lastItemAdded);
    }
    ArrayList<Item> playerInventory = new ArrayList<>();
    playerInventory.addAll(currentPlayer.getItems());

    Intent intent = new Intent(GameActivity.this, InventoryActivity.class);
    intent.putParcelableArrayListExtra("ITEMS_FOUND", itemsFound);
    intent.putParcelableArrayListExtra("PLAYER_INVENTORY", playerInventory);
    intent.putExtra("CURRENT_PLAYER", currentPlayer);

    inventoryActivityResultLauncher.launch(intent);
}```
So in my main activity, when i need to call this activity i use            launchInventoryActivity(currentPlayer); Finally, in my 2nd activity, when it's time to send back info to my first activity, i use this with, i guess, the correct intents we need to get :


    ```private void finishTurn() { int currentPlayerIndex = getIntent().getIntExtra("CURRENT_PLAYER_INDEX", 0); Intent intent = new Intent(); intent.putExtra("CURRENT_PLAYER_HEARTS", currentPlayer.getHearts()); intent.putExtra("CURRENT_PLAYER_INDEX", currentPlayerIndex + 1); intent.putExtra("CURRENT_PLAYER", currentPlayer); // Ajoutez le joueur actuel ici setResult(RESULT_OK, intent); finish(); }```
1

There are 1 best solutions below

0
Daniel Hernández On

In your launchInventoryActivity you are not launching the intent with startActivityForResult so it never enters on the onActivityResult method.

Your launch activity method should look something like this:

    private void launchInventoryActivity(Player currentPlayer) {
        //...
        // remove this line
        inventoryLauncher.launch(inventoryIntent);
        // change it for this
        startActivityForResult(inventoryIntent, REQUEST_CODE);
    }

Take in consideration startActivityForResult its deprecated and so is onActivityResult

more info here: OnActivityResult method is deprecated, what is the alternative?