nic-delhi / AarogyaSetu_Android

Aarogya Setu Android app native code
https://www.aarogyasetu.gov.in/
Other
2.88k stars 1.93k forks source link

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

Open akshaydmc opened 4 years ago

akshaydmc commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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

akshaydmc commented 4 years ago

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

rohanrmallya commented 4 years ago

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

ArjitRout commented 4 years ago

duplicate of #143

skccccc commented 4 years ago

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("", "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 commented 4 years ago

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 commented 4 years ago

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