Removing SurfaceView from the application stack - android

Hello StackOverflow Users,
I am new to android and trying to develop a game in which I use a
1) Main class to redirect (like a menu.. new game, options, help, exit etc..)
2) A surfaceview class
3) A thread to handle drawing on canvas.
I have added an exit button on the main class.
However after playing the game i.e. drawing the objects and using them, when i redirect to my Main class and try to exit; the main screen disappears but the view and threads aren't destroyed.
This is the main class.
package com.tgm.welcome;
import com.tgm.R;
import com.tgm.main.GThread;
import com.tgm.main.TGMActivity;
import com.tgm.options.OptionsMain;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Welcome_Act extends Activity {
ImageView game, exit, options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
game = (ImageView) findViewById(R.id.newGame);
options = (ImageView) findViewById(R.id.options);
exit = (ImageView) findViewById(R.id.exit);
game.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
gotogame();
}
});
options.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goto_opt();
}
});
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
exit_game();
}
});
}
public void gotogame() {
Intent game = new Intent(Welcome_Act.this, TGMActivity.class);
startActivity(game);
}
public void goto_opt() {
Intent opt = new Intent(Welcome_Act.this, OptionsMain.class);
startActivity(opt);
}
public void exit_game() {
System.exit(0);
}
}
PLEASE HELP TO REMOVE THE GAMESCREEN FROM THE STACK THAT ANDROID MAINTAINS.
Thanks..

Using System.exit(0) is not advised in android. It doesnt guarantee finishing the Activity.
Instead of
public void exit_game() {
System.exit(0);
}
Use:
public void exit_game() {
Welcome_Act.finish();
}

Just call finish on the activity you want to remove from the stack.. it does the work you want..

Related

Custom Dialog Window/Pop up window keeps rejecting button input?? Android Studio

So I created a pop-up dialog window to serve as the tutorial for the user, I decided to run tests on it since it was working well but saw that when you added an active button to it, it would not work. Each time I pressed the button that brings up the tutorial(which worked before the addition of the button code), the addition I made with the button taking the user to another part now renders that button inactive and instead takes the user back to the start screen when the tutorial button is pressed.
This is the code I used for it:
package com.example.rockpaperscissors;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import static com.example.rockpaperscissors.R.id.tutorialButton;
public class MainMenu extends AppCompatActivity {
Button tutorialButton, playGameButton, testbutton;
Dialog tutorial_popup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tutorial_popup = new Dialog(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main_menu);
playGameButton = findViewById(R.id.playGameButton);
tutorialButton = findViewById(R.id.tutorialButton);
testbutton = findViewById(R.id.testbutton);
tutorial_popup = new Dialog(this);
playGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
testbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
}
});
}
}
As you can see, there's another setOnClickListener within the tutorial which is basically supposed to take the user to another class/activity when pressed on. However, it does not do it and instead just ignores it, showing that there is an error.
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
testbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
}
});
Is there something I missed when doing this? Please let me know how I can fix this.
Try setting the onclick on the button before showing the pop up
testbutton.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) {startActivity(new Intent(MainMenu.this, MainGame.class));}}); tutorial_popup.show();
So I found a solution to this answer, being that there was actually no need to add a buttonListener to the pop-up dialog as not only was it unnecessary, but it was also breaking the code. With how pop-up dialogs work in Android Studio, I realised through watching videos that you are able to click off of it without needing an "X" button or whatsoever, so I removed the buttonListener and tried again. Safe to say it ran perfectly this time with no problems whatsoever.
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
}
});

My code won't recognise my class when navigating to a second screen

I'm really stuck here. I'm trying to navigate to a second activity using a button but whenever i try to parse in the name of the class to the Intent method, Android Studio throws an error.
In the Intent method toWeightsScreen, it won't let me parse in a class.
Can anyone tell me what I'm messing up please.android studio snapshot
package leith.comstephen.facebook.httpswww.fitnessapp5;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button navToWeightsScreen = (Button)findViewById(R.id.Firstweights);
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(this, cut.class)
startActivity(toWeightsScreen);
}
});
}
}
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(getActivity(), cut.class);
startActivity(toWeightsScreen);
}
});
The this in your intent declaration refers to the button's setOnClickListener.
You should specify the actual activity. See the code below
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Changed cut.class to Cut.class to follow coding conventions
Intent toWeightsScreen = new Intent(MainActivity.this, Cut.class);
startActivity(toWeightsScreen);
}
});

Button doesn't work on a new thread

Previously I had error - something about some loop, I've seen info that it is necessary in that case to start a new thread for button, but still nothing happens, thought logs show no errors now.
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int CAPTURE = 9003;
Button button;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, "BUTTON clicked", Toast.LENGTH_LONG).show();
Log.d("BUTTON","CLICKED");
}
});
}
});
Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(CaptureActivity.AutoFocus, true);
startActivityForResult(intent, CAPTURE);
}
}
runOnUiThread mean you are telling the UI thread to execute the passed Runnable as instruction so it won't create a new thread.
Solution : I assume you this code is for demo purpose for threading and UI updation so one of best alternative is to AsyncTask
and remove runOnUiThread function , no need of it
You need to remove this code or move it inside run method
Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(CaptureActivity.AutoFocus, true);
startActivityForResult(intent, CAPTURE);
Because you are currently in CaptureActivity not on MainActivity.
OnCreate will directly take you to the CaptureActivity where you are expecting the code of MainActivity to run (probably they have same UI)

onClickListener not working that was copied from another working activity

I have created a new activity, added it to my manifest file and copy and pasted code from another fully functioning activity yet my buttons do not work when I click on them. Here is my activity:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class test extends Activity {
private Button btnChangeDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salesticketoilui);
mainProgram();
}
public void mainProgram() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
} // end onClick
}); // end setOnClickListener
Button buttonExit = (Button)findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
exitActivity();
} // end onClick
}); // end OnClickListener
// setup button listener for saving data and exit to main
Button buttonSaveExit = (Button) findViewById(R.id.buttonSaveExit);
buttonSaveExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveExit();
} // end onClick
}); // end OnClickListener
} // end MainProgram ()
public void saveExit() {
// does stuff
}
public void exitActivity () {
// does stuff
}
} // end class
any thoughts?
Based on the code you have shown, it doesn't appear that you ever call the method mainProgram so your click listeners will never actually get setup. Either call mainProgram from onCreate or just put that code directly into onCreate.
I believe the onClickListeners need to go inside of your onCreate method.
Listen to Scott
and looks like your missing the #Override
new View.OnClickListener() {
#Override
public void onClick(View view) {
exitActivity();
} // end onClick
}
Make sure your java settings are to 1.6 to avoid code completion missing this.
I had the same problem copying OnClickListener with ImageButtons from a class to another class, and renaming then with bulk copy/paste.
To make it work, I had to create new buttons in my layout and declare the events manually. Weird!

How can I get a sound file to play on each click of a button in Android?

I'd like to play a sound as I tap a button like hitting a drum, to have the sound playback as quick as I'm hitting the button. Right now I have to wait for the sound to finish before it will play again. Here's how I've got the sound set up.
MediaPlayer snareMP;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playdrums);
final MediaPlayer snareMP = MediaPlayer.create(this, R.raw.snare);
ImageView snareDrum = (ImageView) findViewById(R.id.snare);
snareDrum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
snareMP.reset();
snareMP.prepare();
snareMP.start();
}
});
Any help is greatly appreciated.
Thanks!
Try this:
public void onClick(View v) {
if (snareMP.isPlaying()){
snareMP.stop();
snareMP.reset();
}
snareMP.prepare();
snareMP.start();
}
I'm going to share a code that I've written a while back which I think does the exact same thing you're looking for; I hope it helps and I'd appreciate some rep points if it does :)
Here it is:
import com.myProject.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class Camera extends Activity {
private MediaPlayer snapMP;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera);
Button snap = (Button)findViewById(R.id.snapButton);
snap.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
freeSnapMP();
snapMP = MediaPlayer.create(Camera.this, R.raw.camera_snap2);
snapMP.start();
}
});
}
protected void onDestroy() {
super.onDestroy();
freeSnapMP();
}
private void freeSnapMP(){
if (snapMP != null){ snapMP.stop(); snapMP.release(); snapMP = null; }
}
}

Categories

Resources