App Back Press Twice to Log Out - Not Working Properly - java

I have a bit of a weird one that I can't quite figure out.
When I log out of my app via the navigation menu, it takes me back to the login screen. I have then set it up so that when the user presses the back button twice, it should close the app completely. I have got the toast to appear after one press to say the user needs to press twice but here's my issue.
When I press back twice, the screen clears but the login screen pops back up again. With my last login details in the boxes. Then if I press back twice again, it closes the app completely. I need to try and figure out why it won't close the app on the first two presses of the back button.
Below is the code I am using:
public void onBackPressed() {
//moveTaskToBack(true);
if (!isUserClickedBackButton){
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
isUserClickedBackButton = true;
} else {
System.exit(0); // exits right out of app
super.onBackPressed();
}
}
I have tried not using 'super.onBackPressed', I've tried using it on its own. I've tried adding 'finish()' or just using that on its own. I'm at a loss. Has anyone got any ideas?
I'm using Firebase for authentication if that makes any difference.
Thanks in advance.

You can use:
super.onBackPressed();
And add in the AndroidManifest.xml to the login activity tag:
android:noHistory="true"

You Can do it without using no History in android manifest if you want...
//Use this as class variable..
boolean doublePressedBackExit = false;
#Override
public void onBackPressed() {
if (doublePressedBackExit) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
return;
}
this.doublePressedBackExit = true;
Toast.makeText(getApplicationContext(), "Press again to exit..", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doublePressedBackExit = false;
}
}, 2000);
}

Related

How to exit an app when Back Button is pressed

This is a small question but I'm finding very hard to solve this. I have multiple activities. For example A B and C. When app is launched A is opened and on click of button will redirect me to activity B and then from B on click of another button will take me to C. On Back pressed will now bring me back to B and again on Back button is pressed will take me to A which is main Activity.
Now if I press back button, instead the app should exit...it creates loop between B and A and never exit the app.
I already used the following method
Method 1:
use of this.finishonBackPressed which didn't help
Method 2:
use android:nohistory = true in manifest
But If I do so then from C activity it will directly take me to A which I don't want.
Method 2.
Use of
Intent intent = new Intent(Intent.ACTION_MAIN);
//to clear all old opened activities
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
when I use this then everytime this opens up in my device
Please anyone help.
This is my code in Mainactivity now
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
But also it don't work and it creates loop between A and B Activity.
Your code (in MainActivity) is incorrect , put finish() after super.onBackPressed()
it must be :
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity(); // or finish();
}

Android doesn't close on System.exit()

I am trying to close my App when i press on the Back Button, all it does is just going back to Launcher Activity.
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
System.exit(0);
finish();
}
I tried both, System.exit(1) and System.exit(0), together and each other alone. But nothing works as intended.
EDIT:
I have changed it to
linearLayout_wrapper.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
scrollView.addView(linearLayout_wrapper);
Log.i("Main", "initialize menu layout: complete");
}
protected void onDestroy(){
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
public void onBackPressed() {
finish();
}
And now i get an exception when i press the back button
java.lang.IllegalStateException: ScrollView can host only one direct child
finish() Launcher activity when rise this activity
then you dont need write anycode onBackPresed
read this post for close app: Is quitting an application frowned upon?

Make application quit Android

Hey guys I'm trying to make a function that quit my application when onClick on a Button but it doesn't work.
Would you take a look and let me now what's wrong with the code please ?
Here is the code :
public void addListenerOnButtonLeave()
{
quitButton = (ImageView) findViewById(R.id.quitButton);
quitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
moveTaskToBack(true);
}
});
}
I put this function into the overrided onCreate().
Thanks for your help guys.
You may also use finishActivity (int requestCode)
Force finish another activity that you had previously started with
startActivityForResult(Intent, int).
Probably some of your activities are running in background. When you have switched between activities, you have not finished them.
First finish those activities by adding
ActivityName.this.finish()
just before you sail to another activity, and then use
getActivity().finish();
System.exit(0);
to quit the app triggered where needed.

On physical back button pressed how to go to device's home screen? [duplicate]

When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?
In my Home Activity I override the "onBackPressed" to:
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
so if the user is in the home activity and press back, he goes to the home screen.
I took the code from Going to home screen Programmatically
Immediately after you start a new activity, using startActivity, make sure you call finish() so that the current activity is not stacked behind the new one.
EDIT
With regards to your comment:
What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you can do if you really want to, is to make sure that every startActivity leading up to that activity, is a startActivityForResult and has an onActivityResult listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in that activity, and finish that one, etc...
A better user experience:
/**
* Back button listener.
* Will close the application if the back button pressed twice.
*/
#Override
public void onBackPressed()
{
if(backButtonCount >= 1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
The app will only exit if there are no activities in the back stack. SO add this line in your manifest android:noHistory="true" to all the activities that you dont want to be back stacked.And then to close the app call the finish() in the OnBackPressed
<activity android:name=".activities.DemoActivity"
android:screenOrientation="portrait"
**android:noHistory="true"**
/>
Why wouldn't the user just hit the home button? Then they can exit your app from any of your activities, not just a specific one.
If you are worried about your application continuing to do something in the background. Make sure to stop it in the relevant onPause and onStop commands (which will get triggered when the user presses Home).
If your issue is that you want the next time the user clicks on your app for it to start back at the beginning, I recommend putting some kind of menu item or UI button on the screen that takes the user back to the starting activity of your app. Like the twitter bird in the official twitter app, etc.
Use onBackPressedmethod
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
This will solve your issue.
First of all, Android does not recommend you to do that within the back button, but rather using the lifecycle methods provided. The back button should not destroy the Activity.
Activities are being added to the stack, accessible from the Overview (square button since they introduced the Material design in 5.0) when the back button is pressed on the last remaining Activity from the UI stack. If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.
Your app is responsible to stop any background tasks and jobs you don't want to run, on onPause(), onStop() and onDestroy() lifecycle methods. Please read more about the lifecycles and their proper implementation here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
But to answer your question, you can do hacks to implement the exact behaviour you want, but as I said, it is not recommended:
#Override
public void onBackPressed() {
// make sure you have this outcommented
// super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
To exit from an Android app, just simply use.
in your Main Activity, or you can use Android manifest file to set
android:noHistory="true"
finish your current_activity using method finish() onBack method of your current_activity
and then add below lines in onDestroy of the current_activity for Removing Force close
#Override
public void onDestroy()
{
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
I modified #Vlad_Spays answer so that the back button acts normally unless it's the last item in the stack, then it prompts the user before exiting the app.
#Override
public void onBackPressed(){
if (isTaskRoot()){
if (backButtonCount >= 1){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}else{
super.onBackPressed();
}
}
you can simply use this
startActivity(new Intent(this, Splash.class));
moveTaskToBack(true);
The startActivity(new Intent(this, Splash.class)); is the first class that will be lauched when the application starts
moveTaskToBack(true); will minimize your application
Add this code in the activity from where you want to exit from the app on pressing back button:
#Override
public void onBackPressed() {
super.onBackPressed();
exitFromApp();
}
private void exitFromApp() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}

How to disable a button from another activity?

So, I have a menu activity and there a user can choose a game to play. When he does that, in game he has an option to go back to menu (either on backPress or on my Exit button in game). I need, when that happens, to disable that game button, so the user cannot play it again. How can I do that?
I tried to make an object from Menu class and then disable the button but get error. Also tried:
public void disableButton(){
button.setEnabled(false);
}
and then calling it:
obj.disableButton
But I get stactOverflow errors on line where I created my obj as soon my menu activity starts. How to do this?
EDIT:
In Menu class:
public void disableButton(){
button.setEnabled(false);
}
In Game class:
Menu objMenu = new Menu();
And then in exit onClickListener:
objMenu.disableButton();
EDIT2:
Here's how I call my game activity:
Intent i = new Intent(this, Game.class);
startActivityForResult(i, GAME);
Then in game activity:
Intent resp = new Intent();
resp.putExtra("score", numberOfPoints);
setResult(1, resp);
finish();
And again in Menu class:
final private static int AS = 1;
#Override
protected void onActivityResult(int reqCode, int respCode, Intent i) {
if(respCode == 1) {
switch(reqCode) {
case AS: receivedA = i.getIntExtra("score", receivedA);
button.setEnabled(false);
break;
}
So, this is what I do when game ends, and I disable that button succesfully.
I think the issues is not only in button.setEnabled(false);. As it is hard to say what is the problem without seeing code's details, I am suggesting another solution
start the game activity from menu activity using startActivityForResult
when the game is finished ,in the return intent put a specific data
In Menu activity, in onActivityResult check if the result intent has that specific data
basing on step 3 you can enable or disable the button

Categories

Resources