Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pressing "Back" Button closes App(High Priority BUG) #173

Open
akshaydmc opened this issue May 27, 2020 · 9 comments
Open

Pressing "Back" Button closes App(High Priority BUG) #173

akshaydmc opened this issue May 27, 2020 · 9 comments

Comments

@akshaydmc
Copy link

akshaydmc commented May 27, 2020

There is problem with Intent that doesn't save the last activity because of that the app directly closes rather than going back to last running activity. like in FAQ section when we try to get back to last activity it closes the whole application.

We need to use Intents to "switch windows". To launch another Activity from our current Activity, simply create an Intent with a few parameters and call startActivity() with that Intent. Here's an example:

Intent i = new Intent(this, TheNextActivity.class); startActivity(i)

Don't forget to include your second Activity in the Android manifest file. All Activities in your application must be included in that file.

Other things to note is that we don't really use System.exit() in Android. Just call finish(). It's advised to let Android manage applications and its resources rather than doing it yourself, but if we want to make sure that your application really is shut down, feel free to use System.exit() anyway. There's also no need for overriding onBackPressed() if you're only calling finish(). That's standard behaviour in Android when we hit the back button.

Also, you don't call setContentView() more than once per Activity. You start a new Activity when you need to change the visuals (or use one of the specialized Widgets to switch between layouts.

This also explains why we're experiencing your "problem". You may have changed the layout of the Activity using setContentView(), but there's still only one Activity running - when you call finish(), that Activity gets closed. If you had started a second Activity with a different layout, like you're supposed to do, Android would have closed that second Activity and would have returned you to the first.

@iamsh4shank
Copy link

@AkkiBond I think this can be fixed by raising the alert dialog before closing the app or showing a toast notification if the user presses back button first time like "Press again for closing the app"

@akshaydmc
Copy link
Author

Yes But it should only be when user presses back button two times. Otherwise what is the use of back button if we can't go back to our last activity.

@iamsh4shank
Copy link

@AkkiBond yes on first press it will show the toast and then it will ask for pressing it again.

@akshaydmc
Copy link
Author

@robustTechie I'm trying to trace that problem and fix it with a PR.

@rohanrmallya
Copy link

rohanrmallya commented May 27, 2020

Open Discussions in #7 , #29, #42 , #64, #110

@ArjitRout
Copy link

duplicate of #143

@skccccc
Copy link

skccccc commented May 27, 2020

Developer should not display URL's on network failure. You can hide such webview based errors by display static web page on getting any webpage access error:

webView.setWebViewClient(new WebViewClient() {
@OverRide
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadData("<style>body{background:#022D48;width:100%;height:100%;}</style>", "text/html", null);

        }

}

Also you can minimize the app on pressing back button..

@OverRide
public boolean onKeyDown(int keyCode, @nonnull KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
moveTaskToBack(true);
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Same replied on #143

@rishabhmahla161
Copy link

Or we can just use the addToBackStack() and that would make sure we go back to the previous activity and if there is none the app exits.

@aravindvnair99
Copy link

@AkkiBond Please check for existing issues before opening a new one. You can close this and proceed there. Duplicate of #7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants