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

Error creating a Master/Detail Flow app in Android Studio 1.4
How to handle intent:// on a webView URL?
Implement Tap to Focus in Camera2 API
Troubles while using Android Studio in Mac
onClickListener in ViewHolder is not called in android pre lollipop
getActivity returns null in ActivityUnitTestCase
Showing custom html on WebView errors leaks the real page
programmatically register Receivers and services in android
Playing Background Music in Android in a Simple Game
Android Mediation integration with inmobi , mopub
When i press a back button on my first fragment, the app should exit
calendar date selection for launching new activity
Using fontawesome (icons) in game, in theory
How does vysor create touch events on a non rooted device?
Appium: Selecting between multiple WebViews inside a hybid app
setOnItemClickListener for activity extending ListActivity without adapter

Categories

HOME
notepad++
phpword
linkedin
depth
jwplayer
package
ms-access-2016
android-json
google-cloud-datalab
jtextpane
vert.x
character
pcl-crypto
openstreetmap
heap
text-mining
java.util.logging
activecollab
php-5.6
automatic-ref-counting
internationalization
alpacajs
mathprog
google-content-api
google-data-studio
foreign-keys
digital-signature
poedit
undertow
babel
gulp-watch
r-mice
aggregation
epsilon
roundup
expressionengine3
jquery-multiselect
has-and-belongs-to-many
bringtofront
bioinformatics
finagle
weather
r-grid
papaparse
openalpr
android-augmented-reality
nohup
node-mssql
metaprogramming
jvmti
comparison-operators
levenshtein-distance
sharp-snmp
print-css
phpdbg
gitlist
user-defined-fields
vugen
appdomain
backbone-collections
swingworker
materialized-path-pattern
rocks
simplepie
resourcemanager
google-query-language
deep
beanstalk
concurrentmodification
video-player
sapscript
dulwich
botan
java-security
exceldatareader
jbake
sip-server
jama
insight.database
kissfft
websphere-esb
gwt-openlayers
visual-studio-express
hibernate-entitymanager
mvc-editor-templates
pclzip
http-status-code-410
fb.ui
twisted.web
nokia-s40
monotouch.dialog
multiplexing
usertype
koken
twitterizer
presentation-layer
luajava
timertask
non-clustered-index
hbm2java
pos-for-.net
burndowncharts
visualj#
sifr3
wcf-proxy
leader

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