Hey guys I am trying to create a game where when a condition is met the the Activity is called and now runs level 2 conditions, and so on as the levels increment. The problem is that the level integer which stores the level always resets to the initialised value, I basically want to call the intent if the level up condition is met, reload the activity and increment the level integer.

I cannot figure out how to do this, here is my activity which I want to call:

package com.deucalion0;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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>= 10 && level == 1 || counter>= 15 && level == 2)
		{	 
			Intent openSplash = new Intent("com.deucalion0.FIRSTONE");
			startActivity(openSplash);
			
			level++;
		}
		
		 else if(counter<10 && level == 1 || counter< 15 && 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;
  		}
        
    	
    	
    }
    
    
}

I would appreciate any help at all.

Many thanks!

Dani AI

Generated

Brief summary for and a correction for : the level field is resetting because each Intent start creates a new Activity instance, and instance fields are reinitialized in onCreate. Also calling startActivity from a raw Thread is unsafe — activity lifecycle and UI work belong on the main thread. Creating a custom constructor for an Activity will not work: the Android framework instantiates Activities and will not call a user-defined constructor with parameters. Pass data with Intent extras, save instance state, or use a central store instead.

A simple, reliable pattern:

  • Put the new level into the Intent when launching the activity.
  • Read that extra in onCreate (with a default).
  • Schedule delayed work on the main thread (Handler.postDelayed or CountDownTimer) rather than Thread.sleep.

Example (intent extras + finish previous instance):

Intent i = new Intent(this, FirstOneActivity.class);
i.putExtra("level", level + 1);
startActivity(i);
finish(); // prevents many stacked instances

Read the value in onCreate:

level = getIntent().getIntExtra("level", 1);
lvl.setText("Level is " + level);

Replace the custom Thread/sleep with a main‑thread delayed call:

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
  @Override public void run() { test(); }
}, time);

Other useful options depending on behavior:

  • Use onSaveInstanceState/onRestoreInstanceState to preserve level across config changes.
  • Use launchMode="singleTop" + onNewIntent(...) to reuse the same Activity instance and handle new level extras.
  • For long-lived state across process death, use SharedPreferences or an app-level store (not plain static fields; they can be cleared).

Finally, prefer explicit Intents (new Intent(this, FirstOneActivity.class)) unless an action string has a matching intent-filter in the manifest. These changes avoid the reset problem and keep startActivity calls safely on the UI thread.

Just create a new constructor for your activity that takes integer as parameter. Then when calling on the activity past new level value as parameter of call

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.