Make the button visible with StaticClass and save it

I was still confused in the StaticClass code, which gave my friend an alternative, except for Shared Preferences, for 3 days I tried to find out the code and asked, but there is still a small problem with the code

this is the last following code in my selectlevel.class that I improved

 public class selectlevel extends Activity { Button f1, f2, f3; ImageView f2lock, f3lock; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.selectlevel); f1=(Button)findViewById(R.id.f1); f1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ // TODO Auto-generated method stub Intent level1 = new Intent (); level1.setClassName ("com.example.game", "com.example.game.levelone"); startActivity (level1); } }); f2=(Button)findViewById(R.id.f2); f2lock=(ImageView)findViewById(R.id.f2lock); f2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ // TODO Auto-generated method stub Intent level2 = new Intent (); level2.setClassName ("com.example.game", "com.example.game.leveltwo"); startActivity (level2); } }); updateLevels(); } static class PlayerProgress { private static int progress = 0; public static void updateProgress(int levelNumber) { progress = levelNumber; } public static int getPlayerProgress() { return progress; } } public void updateLevels() { int progress = PlayerProgress.getPlayerProgress(); switch(progress) { case 1: f2.setVisibility(View.VISIBLE); f2lock.setVisibility(View.GONE); break; case 2: break; // You can expand this to as many levels you'd like. } } 

and I used this in my levelone.class to send update progress to 1

 button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v){ selectlevel.PlayerProgress.updateProgress(1); finish(); 

but when levelone.class finished, the f2 button is still GONE and f2lock is still VISIBLE

enter image description here

nothing changes in selectlevel.class

Interestingly, it can be seen like this one and still visible if the game opens again because the visibility of the button is saved.

enter image description here

Can someone help me fix a problem in my code? or give an explanation with another code as a solution?

+2
source share
1 answer

try calling the updateLevels() function also in your onClick function as follows:

 button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v){ selectlevel.PlayerProgress.updateProgress(1); selectlevel.PlayerProgress.updateLevels(); finish(); } 
+3
source

All Articles