Search code examples
androidif-statementradio-buttonsettext

If statement only runs fully onresume()


I'm trying to setup a combat system that allows the user to pick between a melee attack and ranged attack (pick one of two radio buttons). Based on variables what actions the buttons can say, "out of range", "attack melee", "attack range", "reload range". onresume radioButtons have the correct text. After firing ranged weapon for the first time (combat starts with ranged weapon already reloaded) The text for rangeRadio changes to "reload range" but wont change back from "reload range" to "attack range" even when the weapon is 'reloaded' and will fire dealing damage when I end the turn (and preform set actions). If the weapon takes 2 turns to reload. I spend 2 turns reloading, click home button then return to activity it will setText correctly and say "range attack", other wise it will stay "reload range".

finds radioButtons, if statements to first check if there is a ranged weapon equip(rangeId == 50 means no weapon) then check to see if weapon is loaded (int rangeReload = 100), then finally check to see if in range to attack/fire.

    private void setActions(){
         RadioButton meleeRadio = (RadioButton) findViewById(R.id.meleeRadio);
         RadioButton rangeRadio = (RadioButton) findViewById(R.id.rangeRadio);
            if (meleeRange >= distance){meleeRadio.setText(meleeString);}else{meleeRadio.setText(oorString);}
            if (rangeId == 50){rangeRadio.setText(norangeString);}else{if(rangeReload<=99){rangeRadio.setText(reloadString);}else{
            if (rangeRange >= distance){rangeRadio.setText(rangeString); Log.e(rangeString, "Range Attack called");}else{rangeRadio.setText(oorString);}}}
    }

I call setActions(); in two places. in onresume via preCombat() -> layoutcombat()

@Override
protected void onResume() {     
    new pullCombatActions().execute();
    new prepCombat().execute();
    super.onResume();}


    private class prepCombat extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            playerBattlePrep();
            findNPC();
            return null;}
        @Override
        protected void onPostExecute(String result) {
            new layoutCombat().execute();
        }
     }

    private class layoutCombat extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            startCombat();
            pullCombatText();
            return null;}
        @Override
        protected void onPostExecute(String result) {
            setActions();
            popStats();
            refreshStats();
            combatStartText();}
     }

the 2nd place I call setActions(); is in my AsyncTask that I run at the end of a combat turn to refresh the screen and show the user what happened via rolls.

private class replaceScreen extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        pullCombatStory();
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        refreshStats();
        refreshCombatStory();
        highLightPlayerActions();
        highLightNpcActions();
        setActions();
        }
    }

I don't understand why it seems to stop half way through my if statement when setting the rangeRadio but onResume lets it complete all the way and display the proper text for rangeRadio.


Solution

  • Just a suggestion, try moving

         RadioButton meleeRadio = (RadioButton) findViewById(R.id.meleeRadio);
         RadioButton rangeRadio = (RadioButton) findViewById(R.id.rangeRadio);
    

    Outside of your onResume method, and put it on your onCreate (or onActivityCreated if it is a fragment) method and put rangeRadio and meleeRadio as private variables in your activity (or fragment)

    Then, since you are using chained AsyncTasks I'm not sure if all of your onPostExecute are running on your UI thread, so I would try to encapsulete all your calls to rangeRadio.setText() on a handler like this:

    On your on Activity onCreate:

    handler =  new Handler();
    

    then when setting your text do :

    handler.post(new Runnable() {
    
                public void run() {
                    rangeRadio.setText("...");
    
                }
            });