android


Android - Custom back button (back stack)


I have implemented my own back stack but I'm not sure what is wrong or what I could improve, my scenario: I have a project with 2 activities, the first one is the "SplashActivity" - where I load some network data - the second one, the MainActivity.
Inside of my MainActivity I have a fragment and inside of this fragment a webview.
The back button should behave like:
When the user doesn't navigate inside of my webview, close the app.
When the user navigates in webview, use the back history of the browswer.
Here is my code:
#Override
public void onBackPressed() {
Log.d("lastfragment", String.valueOf(fragmentStack.lastElement().getId()));
if (fragmentStack.size() >= 2) {
// implement normal behavior?
Fragment activeFragment=fragmentStack.lastElement();
FragmentTransaction ft = getFragmentManager().beginTransaction();
activeFragment.onPause();
ft.remove(fragmentStack.pop());
Fragment returnToFragment=fragmentStack.lastElement();
String name = returnToFragment.getClass().getName();
if(name=="SplashScreen" || name=="LoginFragment"){
// close?
}
else {
// implement normal behavior?
returnToFragment.onResume();
ft.show(returnToFragment);
ft.commit();
}
}
else {
//close ??
}
}
Try to this code i hope solved this your problem.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(FragmentTransaction.TRANSIT_FRAGMENT_OPEN, YOUR_FRAGMENT_OBJECT);
fragmentTransaction.addToBackStack(null); // Your fragment add to back stack
fragmentTransaction.commit();

Related Links

How to send a message to paired device by bluetooth
How getIntent.hasExtra method works in android
Load more data from Json in RecyclerView on Android
java.lang.IllegalStateException: Could not execute method for android:onClick
Log events Firebase Analytics for Android
edit apk decompiled code
How To Get A Part Of String?
How to prompt the user to choose default navigation application
Device specific bug with CompassOverlay?
Is it okay to use the same layout for two different activities?
Android: Variable has two states
Android TV | How to Center ListRow Items within a parent view (HorizontalGridView)
Is there any way can let greenDAO support android data-binding now?
how to get a boolean value of checkbox whether it is checked or unchecked- android studio?
Android. Gradle build error: Error parsing XML: not well-formed (invalid token)
Surface Texture object is not getting the frames from a Surface Class

Categories

HOME
sonarqube
twitter-bootstrap-3
webdriver
saml-2.0
apiblueprint
tivoli
nsbundle
tail
gembox-spreadsheet
babeljs
crystal-reports-2013
rcloud
sendgrid-api-v3
mql5
jquery-ui-sortable
line
x-cart
datastage
gource
cumulocity
heat
asp.net-core-webapi
googlebot
visual-glitch
articulate-storyline
ios10.2
visual-studio-extensions
object-storage
mount
data.stackexchange.com
maze
pylons
amazon-quicksight
sqlpackage
zend-debugger
pdw-file-browser
arduino-ide
imagenet
sessionstorage
health-monitoring
tasm
odoo-website
tcc
contract
flask-login
httr
x++
serde
rserve
reindex
custom-font
opengl-4
headless
check-mk
jtree
ampps
structuremap4
javafx-css
pytables
flex-monkey
reporting-services-2012
jboss-tools
jquery-jscrollpane
clipboard.js
google-query-language
angular-ui-typeahead
uiactionsheet
database-relations
findfirst
jnlua
sapscript
amiga
ocra
canvg
mmwormhole
angular-gettext
mruby
iiop
encryption-asymmetric
personalization
jjaql
spring-remoting
fpdi
synapse
multitasking
ora-06550
artemis
sphinxql
jammer
beaker-testing
process-explorer
merb
route-provider
mantle
bjyauthorize
onmousemove
veracity
subproject
jdb
window-decoration
gobject-introspection
android-holo-everywhere
socketserver
resharper-6.0
purepdf
mathematical-notation
google-wave

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App