简体   繁体   中英

How can i change the value of a passed object from activity one at activity two (android)

I am trying to make a basic app at android studio. Simply i have two activity page and three class.

Class 1 MainActivity.java its belong activity 1

Class 2 MarketActivity.java its belong activity 2

Class 3 CharacterAction.java in this class we have character features

On activity 1 we have 3 button

活动1

Button 1 purpose is attack when you clicked the button 1 character will gain 2 attack power.Our purpose is when you bought item on market activity while character attacked it will gain 2+2 attack power.

At CharacterAction.java class we have these :

private int kilo;            // Weight
private int hareketSayisi;   //Energy
public int saldiriGucu;      //Attack power
public int saldiriBonus = 2; // it specifies how much attack power you will gain in one attack.

public int getKilo() {  return kilo;  }

public int getHareketSayisi() { return hareketSayisi;  }

public int getSaldiriGucu() { return saldiriGucu;  }

public String savas() {
    if (hareketSayisi > 0) {    // if energy > 0
        hareketSayisi--;        // energy - 1
        saldiriGucu = saldiriGucu + saldiriBonus;    // attackpower = attackpower + attackbonus it will add to attack power + 2
        kilo--;     //weight - 1
            return "karakter savasti";     //return character attacked
    } else {
            return "Yeterli hareket yok";     // there is no energy left
    }
}

and MainActivity.java :

characterAction.setKilo(10);            // set the weight 10

characterAction.setSaldiriGucu(0);      // set the attack power 0

characterAction.setHareketSayisi(10);   // set the energy 10

I passed these objects from MaintActivity.java to MarketActivity.Java :

characterAction = (CharacterAction) getIntent().getSerializableExtra("CharacterAction");

There is no problem here.

Let's see activity 2 (Market place)

活动2 It says if you buy these sword your attack power gain will increase 2x at your every every attack.

And my question is starting :

I got onClick() method on MarketActivity.java

@Override
public void onClick(View view) {
    if (view.getId() == btnSatinAl1.getId()) {
        tvSuccesful.setVisibility(View.VISIBLE);

        if (characterAction.saldiriGucu > 10) {    // if attackpower > 10
            characterAction.saldiriBonus = (characterAction.saldiriBonus * 2);    // attack power bonus * 2
            tvSuccesful.setText("Satın alım başarılı");    // Succesful
                    btnSatinAl1.setVisibility(View.INVISIBLE);

        } else {
            tvSuccesful.setText("Yetersiz saldiri gücü");    // Not enough attack power.
        }
    }
}

On these onClick() method it needs to add +2 attack bonus while succesfully bought the sword.But it wont.On the MarketActivity.java it says Succesfully Bought but when i turn Activity 1 and trying to attack its still giving +2 attack power for each attack.

How can i increase these ?

Thanks for reading.

There are few possible ways that you can resolve this, I would start the MarketActivity with *

startActivityForResult()

Way 1:

Use cache, in MarketActivity update object and save updated in cache, next in first Activity you should update your object from cache for example wtih:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode to resultCode) {
            Navigator.XXX_REQUEST_CODE to Activity.RESULT_OK -> {
                refreshDataFromCache()
            }
        }
    }

Way 2:

If you don't want to use cache, pass updated object in result intent's data and then update object and next UI:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode to resultCode) {
            Navigator.XXX_REQUEST_CODE to Activity.RESULT_OK -> {
                data?.let {
                    val xxx = Parcels.unwrap<XXX>(data.getParcelableExtra(xActivity.DATA_TAG))
                    item = Parcels.unwrap<XXX>(data.getParcelableExtra(xActivity.DATA_TAG))
                }
            }
        }
    }

Parcels.unwrap is from https://github.com/johncarl81/parceler

I solved my problem with changing the public int saldiriBonus = 2; // attack bonus to public static int saldiriBonus = 2; All i do this. Changing the method to static . .

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