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

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
How to set 2 launchers for an application?
How do I restart this wallpaper engine after settings have been updated?
Getting data from Google Play Leaderboards
Android Viewpager does not refresh on scrolling Tabs
SharedPreferences/Variables not read in order
How to install a previously generated apk in different devices without rebuild?
Python, flask, android app [closed]
react native how to disable android dev mode
Benifits of content_main.xml in android studio 1.4 & later versions
How to set custom refresh view in SwipeRefreshLayout?
Is it ok to recylce a static bitmap?

Categories

HOME
shell
uml
deep-learning
spark-streaming
cheerio
linkedin
apache-jena
devise
ms-access-2016
odoo-8
lanczos
bing-maps
arangodb
atlassian
gruntfile
intershop
vuejs
pyramid
nodemcu
bourbon
database-connection
replication
drive
guzzle
rubymotion
zxing
ms-access-web-app
winexe
gitlab-api
keras-layer
sharding
modalviewcontroller
navigator
swagger-php
hivemq
sessionstorage
xdocreport
linq-to-objects
mongoose-schema
tasm
smoothstate.js
file-sharing
openrasta
wcftestclient
hammer.js
reportservice2010
reindex
py2neo
reachability
change-tracking
visual-studio-2008-sp1
angular2-testing
grails-plugin
stax
jboss-tools
installshield-2010
appscale
refinerycms
android-wake-lock
aurora
project-organization
sklearn-pandas
redis-cluster
linklabel
gocql
fiware-monitoring
jdi
microsoft-reporting
ess
dock
visual-sourcesafe-2005
viola-jones
mraid
node-serialport
dbaccess
webmatrix-3
jbake
insight.database
xml-namespaces
create.js
multiautocompletetextview
windows-embedded-standard
scriptaculous
poker
superscrollorama
swfobject
frameworkelementfactory
pstack
paginator
cgimage
lr
delimited-text
folding
pos-for-.net
filesystemobject
dsl-tools
movieplayer
commercial-application

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