Note that there are some explanatory texts on larger screens.

plurals
  1. POPass a variable to an An activity while using an Intent in Android
    primarykey
    data
    text
    <p>I am creating a simple game which after a 5 second timer expires and two conditions are checked, condition one is to level up, the intent starts the same activity and I intended to increment my variable level by one every time the player met the condition which increased the game level.</p> <p>The second condition starts another activity, which is the fail screen.</p> <p>I didn't want a different screen for every level up increment, when I can just load the same activity and continue going through the conditions, but as the Activity starts again the level variable resets to its initialised value, the game never leaves level 1. Is there any way to pass a variable a value to an activity when starting it?</p> <p>Here is the code I have for my Activity:</p> <pre><code>public class FirstOneActivity extends Activity { /** Called when the activity is first created. */ MediaPlayer ourSong; int counter; Button add; Thread timer; TextView display; TextView lvl; int level = 1; int time; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ourSong = MediaPlayer.create(FirstOneActivity.this, R.raw.click); counter = 0; add = (Button) findViewById (R.id.bAdd); display = (TextView) findViewById (R.id.tvDisplay); lvl = (TextView) findViewById (R.id.lvldisplay); add.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub counter++; //ourSong.start(); display.setText("Your total is "+ counter); if(counter ==1) { set(); timer.start(); } } }); timer = new Thread(){ public void run(){ try{ sleep(time); }catch(InterruptedException e){ e.printStackTrace(); }finally{ test(); } } }; } public void test(){ if(counter&gt;= 10 &amp;&amp; level == 1 || counter&gt;= 15 &amp;&amp; level == 2) { Intent openSplash = new Intent("com.deucalion0.FIRSTONE"); startActivity(openSplash); level++; } else if(counter&lt;10 &amp;&amp; level == 1 || counter&lt; 15 &amp;&amp; level == 2){ Intent openNext = new Intent("com.deucalion0.NEXT"); startActivity(openNext); } } public void set(){ if(level == 1) { lvl.setText("Level is "+ level); time = 5000; } else if (level == 2) {lvl.setText("Level is "+level); time = 5000; } } } </code></pre> <p>Any advice is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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