I need help making my game pause and resume - java

I have a game where you click on an image in the centre, and a value increases. When the app is opened, I've made a splash come up before the main activity starts (the clicking screen). However, every time I back out of the app, and click the icon again, it goes through the splash, goes to the main screen, and starts the game again, setting the value back to zero.
My Java for the Splash:
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity {
#Override
public void onCreate(Bundle splashBundle) {
// TODO Auto-generated method stub
super.onCreate(splashBundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
Thread logoTimer = new Thread(){
public void run(){
try {
sleep(2000);
Intent mainIntent = new Intent("com.bipbapapps.leagueclickerapp.CLICKER");
startActivity(mainIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
My Java for the MainClass which is then run:
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainClass extends Activity implements OnClickListener {
public float goldCount = 0.0f;
Button minionClick;
TextView textGoldCount;
String textTotal;
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainlayout);
//Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
//String which will display at the top of the app
textTotal = goldCount + " Gold";
//Setting TextView to the String
textGoldCount.setText(textTotal);
//Setting onClickListener
minionClick.setClickable(true);
minionClick.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.minioncentreid:
goldCount += 1.0;
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
break;
}
}
}
Anyone have any idea how to allow my game to pause and resume when it's minimized? Also, is there a way so that when the app is destroyed (properly closed), and restarted, the values for variables are kept? Would appreciate your help.

Inside onBackPress of the second activity, you should store the score in shared preferences. Every time you come on the onCreate of Splash activity, retrieve the score value and check if it is set to 0 then show splash screen else goto main activity with current score.

Related

Im new to creating app using java on android studio. Im creating splash screen but not works as i expected

The splash screen should move towards up and down, then go to the next screen. however, it just change the screen normally. the problem i think maybe between the handler and the animate splashscreen. this is my splash.java. hoping anyone can help thank you.
package com.example.dashboard;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.HeaderViewListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.airbnb.lottie.LottieAnimationView;
public class Splash extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
ImageView logo,splashImg;
TextView appName;
LottieAnimationView lottieAnimationView;
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
logo = findViewById(R.id.logo);
appName = findViewById(R.id.appname);
splashImg = findViewById(R.id.img);
lottieAnimationView = findViewById(R.id.lottie);
splashImg.animate().translationY(-1600).setDuration(1000).setStartDelay(3000);
logo.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
appName.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
lottieAnimationView.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
It looks like you start another Activity at the same time you play your animation. Notice the .setStartDelay(3000); at the end of each of your animation calls.
Just removing this will work. However, there are better ways to handle this.
One of the ways would be to override the onAnimationEnd method of your longest animation so that it either changes the Activity immediately or the change is delayed starting from that point in time. This will look something like this:
splashImg.animate().translationY(-1600).setDuration(1000).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
}
});

I want to make password protected android application

I want to make password protected android app, but when in this simple program system is not matching two strings.
package com.pokmgr;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainPM extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pm_layout);
final EditText pin = (EditText) findViewById(R.id.pinET);
final String pass = pin.getText().toString();
final String code = "ajaj";
Button enter = (Button) findViewById(R.id.enterBtn);
enter.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (pass.equals(code)) {
Intent in = new Intent(MainPM.this, Menu.class);
startActivity(in);
}
else {
Intent in = new Intent(MainPM.this, Menu2.class);
startActivity(in);
}
}
});
}
/*#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pm_layout, menu);
return true;
}*/
}
I have made Menu and Menu2 class, but every time Menu2 class is accessed. Even if I enter same pass that is "ajaj" [in this code to test]
i have defined both activities in manifest file.
Can't understand why pass.eqals(code) is not working
The problem is that you are setting pass to the contents of the EditText when the activity gets created. Instead you have to retrieve the contents of your EditText inside the OnClickListener.
Like this:
public void onClick(View v) {
final String pass = pin.getText().toString();
if (pass.equals(code)) {
// do something
} else {
// do something different
}
}
Put pin.getText().toString(); inside onClick of button. You are setting variable pass before the user actually entered something in pinEt EditText.

Android button slow action

I have a button that leads to another page. And whenever I click it I get this info code in logcat :
12-07 16:09:45.073: I/ActivityManager(273): Displayed com.example.prva/.button: +1s764ms
Seconds and ms vary of course each time between 1-3 seconds. The problem is that I noticed that it takes a while for that button to open that page. It has some kind of pause or whatever and this is the only relevant thing I have found in the logcat that could be connected to it. How can I fix this, why is this button acting "slow"?
This is where the button code is :
package com.example.prva;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Meni_Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnv = (Button) findViewById(R.id.buttonv);
btnv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Meni_Splash.this, button.class));
}
});
}
}
And this is the class that opens :
package com.example.prva;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class button extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
//Button click sound
final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio1);
final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.radio2);
final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.radio3);
final RadioButton rb1, rb2, rb3;
rb1 = (RadioButton) findViewById(R.id.radio1);
rb2 = (RadioButton) findViewById(R.id.radio2);
rb3 = (RadioButton) findViewById(R.id.radio3);
Button btn = (Button) findViewById(R.id.buttonplay);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rb1.isChecked())
{
MPRadio1.start();
}
else
{
if(rb2.isChecked())
{
MPRadio2.start();
}
else
{
if(rb3.isChecked())
{
MPRadio3.start();
}
}
}
}
}
);}}
I don't know what thing could make it so slow from these activities?
Your code looks pretty decent to be honest. Not sure entirely what could be causing it to intialise slowly.
But there are two areas to look at.
The first, most likely, is your layout loading:
setContentView(R.layout.button);
I dont imagine your layout to be complex though. But if it is, aka, lots of nested views (linear layouts within other linear layouts), or lots of views (textviews etc) in general on the page, then it could be taking a while to "inflate" the Layout.
Alternatively and less likely, is that MediaPlayer.create takes a fair while to load. The reason I suggest this, is I have no idea how it works, as I've not used it before.
//Button click sound
final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio1);
final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.radio2);
final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.radio3);
The best thing to do, would be to profile it with the DDMS profiler. Or put a timer around it, and print the results to logcat.
Also, on a quick note, is it just 2-3 seconds loading? And is it really that bad for what its trying to do?

android devolpment linking buttons to other xml pages

I'm developing an app for android. The main screen has 6 buttons. Each button leads to another screen. I'm having trouble with the code to make the buttons do anything when clicked. this is what I have:
on the main page my button id is glass the page opened when clicked is glass.xml
android:onClick="Intent i = new Intent(FirstActivity.this, SecondActivity.class);"
and my scr folder I have the java activities FirstActivity.java
package install.fineline;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fine_line);
Button btnload = (Button) findViewById(R.id.glass);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
and SecondActivity.java
package install.fineline;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
}
what am i doing wrong?
On the button definition in the xml remove the onClick attribute.
The button click action is set in code so it doesn't need to be in the xml file.
There is sample code here showing how to do what you want.

Text doesn't show up as it has to! (string)

Ok, thanks to below dude biggest issue is fixed. But whatever I print out, none is printed and I cannot print out the message what is typed in class 2 (nimekysija) as well :(. I really need that it stores name and in future it will write down name every time! Thanks for your help!
Problem must be in 2nd class tho. When I update editor.putString("nimi2", nimiS); nimiS into "plapla", then plapla actually shows up :/. So I have really no idea, what is problem!
(updated below classes too to the newest)
Class 1:
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class MainStuff extends Activity {
TextView tere;
String nimi;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.tvTere);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
if (nimiOlemas == false){
startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
finish();
}
if (nimiOlemas == true){
nimi = preferences.getString("nimi2", "");
System.out.print("töötab!");
tere.setText("Tere " + nimi);
}
System.out.print("töötab2!");
}
}
CLASS 2
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class nimekysija extends Activity {
EditText nimi;
SharedPreferences preferences;
String nimiS;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nimekysija);
preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
nimi = (EditText) findViewById(R.id.etNimekysija);
nimiS = nimi.getText().toString();
Button kysOk = (Button) findViewById(R.id.bNimekysija);
kysOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.Editor editor = preferences.edit();
editor.putString("nimi2", nimiS); // nime kirjutamine
editor.putBoolean("nimionolemas", true); // nimi on kirjutatud!
editor.commit();
startActivity(new Intent("viimane.voimalus.MAINSTUFF"));
finish();
}
});
}
}
Ok I'm guessing you may be new to Java, forgive me if I'm incorrect. You never READ from nimiOlemas.
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
nimiOlemas = false;
startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
finish();
nimiOlemas = true;
I think what you are trying to do is initialize nimiOlemas and then, if it is false, start an activity, call finish, then set nimiOlemas to true, but this is not what you are doing. Is this what you want?
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
if (nimiOlemas == false)
{
startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
finish();
nimiOlemas = true;
}
= is an assignment, == is a boolean comparison. You say in your question that you check the value of your boolean, but you never do, you only assign to it.
Assuming that nimiOlemas is inherited from activity and not used in the activity class, or other supper class, then yes it is not used in nimekysija (Class 2). It IS used in class 1. But this should just be a warning... You will get this warning for every class that extends activity and doesn't use nimiOlemas.

Categories