How to Kill an activity on back button pressed - java

One of my activity displays a down count timer by the help of CountDownTImer() method. The onFinish() method of the Down count timer is displaying a toast to tell the user that the time has elapsed. When it reaches 00:00. The issue is when I press the back button before the time elapses I still get the toast message even if the activity is not visible.
So, I try to kill the activity on the back button pressed by overriding the onBackPressed() as follows, but still, the toast is displaying even if the activity is invisible.
public void onBackPressed(){
super.onBackPressed();
this.finish();
}

Why you don't cancel CountDownTimer() :
public void onBackPressed(){
yourCountDownTimer.cancel();
super.onBackPressed();
}

Related

Activity runs background, although back button is pressed

There is timer in GameActivity. After time finishes, end of round activity runs.
When I press back button in GameActivity, previous Activity (MainActivity) runs. But in background, GameActivity is still running. After time finishes, I see end of round activity screen although I am in MainActivity.
That means game activity runs in background. How can I stop GameActivity when I press back button?
I tried Finish() method.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(GameActivity.this,GameSettingsActivity.class);
startActivity(i);
finish();
}
You should stop your timer in the GameActivity's onPause() method. This will ensure your timer is stopped when hitting the back button but also when your app goes in background.
#Override
protected void onPause() {
super.onPause();
//stop your timer here...
}

Activity with countDown timer pops up after time is up, how to kill it?

I have kind of a game with 2 activities: Start Activity (with high score, start button and tutorial button) and main game activity which is is based on countdown timer, when time is up, game returns to start activity.
Problem is when user starts game and then hits home button and goes back to home screen (just leaving game by home button) Everything is ok until time is up, then Start activity appears on screen with lost message.
I've tried various combined methods like onPause witch finish(); inside and so on but it doesn't work or causes app force close.
I can't handle home button click like in onBackPressed() which I did in that case.
Is there any way to suspend app and pause all threads while it isn't in foreground?
I suggest two way
1
Create global variable like
private isInForgrand = false;
And in onStop() or onPause() and onResume() change it
#Override
public void onStop() {
isInForgrand = false;
super.onStop();
}
#Override
public void onResume() {
super.onResume();
isInForgrand = true;
}
And in onFinish() check it
#Override
public void onFinish() {
if(isInForgrand){
//do what you want
}else{
//your app NOT in Forgrannd
}
2
You can cancel CountDownTimer in onStop()
#Override
public void onStop() {
super.onStop();
mCountDownTimer.cancel();
}

Android back button close popup if opened

I have a main activity , and a popup.xml file that is included in the activity
the problem is when i press the back button , it closes the app directly , whether the popup is opened or not
i got the idea to override the onClick method, add a boolean that will be true when the popup is opened , and false otherwise , then add this condition in the onClick method
i'm still a noob with Android Studio , would anyone please guide me through ?
Thank you.
override the onBackPressed in your activity and check if popup is showing. if popup is showing then close popup else do general back press action
#Override
public void onBackPressed() {
if(popupWindow.isShowing())
popupWindow.dismiss();
else
super.onBackPressed();
}
Just override the following method in your Activity:
#Override
public void onBackPressed()
{
//Do whatever you want before the back button should trigger
super.onBackPressed(); // call this only if you want to close the app
}
#Override
public void onBackPressed() {
if(!(Activity).isFinishing){
//activity is not yet finished
}else{
//activity finishes
super.onBackPressed();
}
}

how to close main activity after closing another activity in android?

I am developing an android application. So in there I have button called "Aboutus". when I click that one it starts another activity and show corresponding view.
Here is the code for button Aboutus click event.
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,AboutUs.class);
startActivity(intent);
}
});
}
And then in that aboutus activity I have back button.when we press that back button it will go to the main activity again.
back button key event goes like this..
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
startActivity(intent);
finish();
System.exit(0);
}
});
here i put some intent extras for some other purposes.In this case they do not matter. So and then again in mainactivity i have exit button. which should kill whole app.
and my exit button code goes like this.
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
System.exit(0);
}
});
Exit button works perfectly (it ends the app) except for one scenario. if we click on aboutus and go to that activity and then press back button on that aboutus activity and then again come back to main activity and finally when i want to exit it won't kill whole app, instead it again goes to the about activity.
in conclusion,
MainActivity---> click aboutus button(no problem in here.this will start aboutus activity)
Aboutus----->click back button(this also works fine. go back to main activity)
MainActivity--> Exit button (not working .it goes to aboutus activity again)
So how to fix this problem?
i dont see why you have to start MainActivity.java ...the user can just hit the back button as long as its on the activity stack. Also The system.exit(0) is bad. Just let the app finish gracefully with finish().
if you want to pass something to the activity when aboutUS is done, you can look up onNewIntent() method.
use intent flag ACTIVITY_CLEAR_TOP to clear all activities on top of it:
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP From the doc:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.

CountDownTimer Text dissapears if a different Activity opened - Timer continues - android app

I have a countdowntimer that starts when a button is pressed.
Once you press the button - the button dims, and text on the button appears and counts down to 0 (when onFinish is reached the button becomes illuminated again and the text reads 'done').
I have a settings menu that allows for the addition of more timers and other settings - if a user starts a timer and it is working fine - then opens the settings menu and saves settings, they come back to the timer screen.
What they see is that the button is grayed out as if the timer is still counting down (which it is), but the text is no longer there counting down and when the timer finishes the button just remains dim.
Is there anyway to get the text counting down to be persistent in that activity even if another Activity is opened up temporarily (like the settings menu) so it will always show the appropriate timer countdown text? I'm still very new to android programming so any examples are appreciated.
TimerCode looks something like this:
//Timer Countdown
#Override
public void onTick(long millisUntilFinished) {
button.setText((millisUntilFinished/1000)+"");
button.getBackground().setColorFilter(android.graphics.Color.GRAY, Mode.MULTIPLY);
button.setTextColor(Color.GREEN);
button.setTextSize(24);
//Timer Finishes
#Override
public void onFinish() {
System.out.println("DONE");
button.setTextSize(44);
button.setText("UP");
button.getBackground().setColorFilter(null);
Button Code looks like this:
//Right Button1
final CountDown rButton1Timer = new CountDown(300000,200,bRightButton1);
bRightButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rButton1Timer.start();
}
});
Ultimately I just want the onTick and onFinish to be persistent through whatever the user does - so if he opens up the settings and changes something, when he comes back to the timers they are still counting down.
Any ideas?
onResume was the culprit. I have since implemented onResume with success.

Categories