Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing an Instance's Variable from another Activity - Android Developement
    text
    copied!<p>I have creted a program that has 3 Activities: MainActivity, UpgradeActivity and UpgradesActivity.</p> <p>Main Activity contains a timer and it also contains an instance of a Vehicle class.</p> <pre><code>public class MainActivity extends Activity { TextView vehicleSpeed, vehicleName, vehicleDistance, vehicleLocation, vehicleStatus, vehicleNews, vehicleInfo, vehicleMoney; ProgressBar vehicleFuel; public static Vehicle vehicle; boolean launched; public static PartType selectedType; Handler handler = new Handler(); </code></pre> <p>I have a button in MainActivity, that when pressed will take me to a page where i can select which part of the vehicle i wish to upgrade. For example i select: Engine. The engine Button takes me to the Upgrade Activity. In this activity i can buy the upgrade which should be applied to the vehicle in MainActivity. For the purpose of this question, lets say it set vehicles speed to +3.</p> <p>My question is in regards to how to access the vehicle instance inside the MainActivity from the UpgradeActivity. I've tried making the instance static but that didn't work. How do i gain access and how can i change the vehicles variables from the other activities.</p> <p>Here is where i am making the instance:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { this.selectedType = PartType.Antenna; this.launched = false; super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vehicle = new Vehicle(); vehicle.setupCar(); </code></pre> <p>Here is where i am accessing the variable in Upgrades, it call the upgrade function inside of the Vehicle Class:</p> <pre><code> buyUp1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { MainActivity.vehicle.upgradeEngine(MainActivity.vehicle.engineLvl + 1); Intent activityChangeIntent = new Intent(UpgradesActivity.this, MainActivity.class); UpgradesActivity.this.startActivity(activityChangeIntent); } }); </code></pre> <p>And this is the function within the Vehicle Class:</p> <pre><code> public void upgradeEngine(int lvl) { engineLvl += 3; engine = parts.getEngine(lvl); } </code></pre> <p>The vehicle Stores an integer called: EngineLvl. This determines what level the cars engine is. The level is incremented by +3 everytime the engine is updated. </p> <p>The problem is that the engine level never changes. Even if i make the Vehicle instance and all of the variables within vehicle STATIC;</p> <p>MAINACTIVITY:</p> <p>Vehicle Button to UpgradesActivity</p> <p>UPGRADESACTIVITY:</p> <p>Button to UpgradeActivity</p> <p>UPGRADEACTIVITY:</p> <p>Change vehicle enginelvl Int Button back to MainActivity</p> <p>Main>Upgrades>Upgrade</p> <p>Thank you for your time</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload