java


Static context saved in Application class and used in a singleton toast builder, does this create a memory leak?


I've got a singleton Toast-showing class, which keeps track of the current toast and cancels it when it is started again:
public enum SingleToast {
INSTANCE;
private Toast currentToast;
private String currentMessage;
public void show(String message, int duration) {
if (message.equals(currentMessage)) {
currentToast.cancel();
}
currentToast = Toast.makeText(App.getContext(), message, duration);
currentToast.show();
currentMessage = message;
}
}
(Extended explanation of this example can be found here: toast issue in android)
The main difference between the answer in the link and this piece of code is the context, which isn't passed as parameter. I get it using static context saved in the App class, which is saved at the startup of the app:
public class App extends Application {
private static Context context;
#Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
}
I've read about this simple context solution in several blogs, and it helped a lot in retreiving context in classes for which passing context as parameters is just annoying, or even impossible.
However, I wonder if I am creating a memory leak!
First of all: does the static context by itself cause a memory leak (if yes, how can I prevent it without having to pass context always everywhere?)
Secondly: is it a problem to use context in a toast object, and save it as a field within a singleton, even if the context itself would not be static?
First
No, if you save context only in Application, as described above, it will not cause memory leak.
About second, It is almost the same to first case, but first is more simple in implementation than second.

Related Links

Reference of a Class ? How does it workout in memory?
message is not sent from server to client in single server - multiple client in java
Create instance of JAXBRIContext
New Object creation best practice (for this situation)
String Format works fine through Java's SimpleDateFormat and not with JodaTime's DateTimeFormat
Building WALA on osx “the import java.lang.invoke cannot be resolved”
netbeans: dependencies for c3p0 and hibernate
calling a method thats located in main class from another class
Spring MVC - upload file is blocked by spring security [duplicate]
Run “Android update project --path .” return invalided number of parameters
Scanner object skipping
CriteriaBuilder in JPA with the method between
readLine for reading multiple lines without a loop?
Force Java runtime to use Jar in NSF instead of Jar on server of same name for XPages application
Java Array Questions
Can't execute servlet from JSP but can access from direct URL (mvc mysql tomcat)

Categories

HOME
pandas
image
hook
reflection
path-finding
nullpointerexception
dot
bpmn
adb
cvs
dxl
opengl-es-2.0
fancybox
mouse
constraint-programming
ios-charts
multiple-records
volttron
floating-action-button
circuit
google-static-maps
serilog
google-cloud-speech
nodatime
css-animations
smb
facet
selectedindexchanged
virtualdub
fluentvalidation
typo3-6.2.x
neo4j-spatial
google-cloud-nl
google-rich-snippets
vsts-build-task
xmlreader
git-merge
retina-display
sharefile
elasticsearch-plugin
eclipse-scout
reportingservices-2005
no-www
thin
prolog-setof
winscp-net
tizen-native-app
worker-thread
android-cursor
nsarray
lync-client-sdk
crosswalk-runtime
quartz-composer
static-ip-address
ado.net-entity-data-model
phishing
mikroc
storekit
moveit
phpcas
qdialog
mutation-observers
wireshark-dissector
twirl
proj4js
device-orientation
javax.mail
bigbluebutton
fscommand
pick
knuth
android-radiobutton
wp-query
undefined-reference
android-2.2-froyo
didselectrowatindexpath
oracle-warehouse-builder
terminal-services
ivyde
quartz-graphics
opcache
jplaton
eclipse-memory-analyzer
padarn
sqlperformance
anonymous-methods
trusted
first-responder
chrono
uiviewanimation-curve
floating
datareader
remember-me
radcombobox
sudzc
fixed-width
brewmp
data-driven
project-hosting
mediarss

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