android


BackPressed in Fragment not functioning properly


I have the following code:
v = inflater.inflate(R.layout.fragment_color, container, false);
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mMenu.isOpened()) {
mMenu.closeMenu();
} else {
}
}
return false;
}
});
When I press the back button, my menu closes like it should, but the app closes as well and it should not.
What am I doing wrong with what I have here? any help would be appreciated.
This is because finishing current activity is back key default behavior. You need to override this behavior by returning true in your onKeyListner. Below code should work for you.
v = inflater.inflate(R.layout.fragment_color, container, false);
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mMenu.isOpened()) {
mMenu.closeMenu();
//It says that you have handled back key
return true;
} else {
}
}
return false;
}
});

Related Links

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
Android ActionBar API, getSupportActionBar() and setDisplayHomeAsUpEnable meaning?

Categories

HOME
sonarqube
grok
prism
awk
jmeter
flex
apiblueprint
devise
grafana
replace
alarm
seo
tivoli
hpc
mapbox
banner
trac
webix
character
fosrestbundle
syntaxnet
fhir
coin3d
scsi
intershop
fileserver
wolframalpha
digital-signature
identity-management
microsoft-ocr
roslyn
draft-js-plugins
siri
beautifier
shared
apktool
wcag
google-cast
utorrent
hard-drive
contacts
hunspell
docfx
wurfl
xdocreport
apache-stanbol
magento-1.9.3
softmax
chronicle-map
knime
apache-spark-dataset
flask-login
plan-9
dynamic-linq
facebook-pixel
manova
podscms
trojan
jqchart
mouseleave
gameplay-kit
choice
ng-grid
freepbx
findfirst
pushbots
jparsec
terracotta
oracle-spatial
extjs-grid
typemock
alloy-ui
bufferedinputstream
jquery-forms-plugin
j-security-check
document-repository
mruby
iiop
ssis-data-flow
xml-namespaces
verold
websphere-esb
solace-mq
ebay-lms
oocss
cocoalibspotify-2.0
mongohq
scmmanager
virtual-pc
resource-management
android-loadermanager
jdb
cgimage
jquery-address
role
u2netdk
lr
http-daemon
codebase
appledoc
svk
diagrams
commerceserver2007
metaweblog
facebook-iframe
easyb
binomial-heap
lobo-cobra
wordprocessingml
complex-event-processing
escrow

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