Android :: Static Fields In Activity Classes Guaranteed To Outlive Create / Destroy Cycle?

Oct 28, 2009

I frequently run into the problem that I have to preserve state between several invocations of an activity (i.e. going through several onCreate()/onDelete() cycles). Unfortunately, Android's support for doing that is really poor. As an easy way to preserve state, I thought that since the class is only loaded once by the class loader, that it would be safe to store temporary data that's shared between several instances of an activity in a static Bundle field. However, occasionally, when instance A creates the static bundle and stores data in it, then gets destroyed, and instance B tries to read from it, the static field is suddenly NULL. Doesn't that mean that the class had been removed and reloaded by the classloader while the activity was going through a create/destroy cycle? How else could a static field suddenly become NULL when it was referencing an object before?

Android :: static fields in Activity classes guaranteed to outlive create / destroy cycle?


Android :: Avoid Non Static Inner Classes In Activity?

Jun 17, 2010

Since I seem to have caught two activity references in a heapdump, where the Activity is set to singleTask. Romain's advice on avoiding memory leaks includes: "Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside"

What does this mean exactly? I can't find any examples, positive, or negative for this rule. I do have some non static inner classes in my activity. Most of them are anonymous inner classes like this one. I see hundreds of them in the samples:....................

View 24 Replies View Related

Android : When To Use Layouts / Create New Activity Classes?

Mar 3, 2010

I'm writing a game for Android. My GUI has the following basic screens (i.e. the information and interactions required on each of these would take up the whole display):

Main menu (let you start a new game, enter settings, see high scores, see about screen)
Game screen i.e. where the actual game is played.
Settings screen.
High scores screen.
About screen (i.e. credits and a back button)
Game over screen (arrived at when the game ends)
Pause screen (pauses game and can access settings)

The following are some example transitions the user might make between these screens:

1->2->7->2->6 (starts new game, pauses game, returns to game, finishes game)
1->5->1->4->1 (views about screen, goes back to main, views score screen, goes back to main)

I'm really confused about when to have just one activity that switches layouts and when to create new activity classes. For example, when my game loads, I have a "main" activity that loads the main menu layout. When you click the settings button, I launch a "settings" activity (which uses android's standard settings GUI). For the moment, when you start a game, I switch the layout of "main" to the game screen layout (which just contains one big surface view). I'm not really sure what the best way to integrate the game over screen, high score screen, about screen etc.

Creating a new activity for each seems really heavy weight to me. There's quite a lot of boiler plate code involved for each activity. Plus, communication between activities seems like a pain as you have to use bundles. Using just one activity means I can just share object fields directly. It seems that using layouts for the above would be more compact. Can anyone give me some recommendations?

View 1 Replies View Related

Android :: Static Vs Non-static Inner Classes

Mar 9, 2009

I have been finding it convenient to extend Handler in many of my activities to handle messages specific to the activity. The handler sublass is an inner class of the activity and needs to access its state. I am wondering if there is any performance difference between making the handler subclass static and passing in the activity explicitly in its constructor or making the subclass an "instance class" and letting the vm worry about my accessing members of the containing activity.

The static approach:

CODE:.............................

View 4 Replies View Related

Android :: Create Static Options Menu For All Activity Screens

Jan 15, 2010

I wanted to create a static options menu for all my activity screens. I dont want to override onCreateOptionsMenu() in each activity. Since Menu class is an interface with a huge number of methods, its difficult to create a static object of the implementing class.

View 3 Replies View Related

Android :: The Life Cycle Of Static

Mar 7, 2010

When starting a new Activity, I want to pass a complex object and do so by using this approach:

MyActivity.COMPLEX_OBJ = myComplexObj; // which is definitely NOT NULL! Intent intent = new Intent(); intent.setClass(this, MyActivity.class); startActivity(intent);

and then in MyActivity:

@Override public void onCreate(Bundle bundle) { if (COMPLEX_OBJ == null) { // report to Flurry ... } ...

}

View 17 Replies View Related

Android :: Removing Public Static Fields In OS Environment?

Sep 20, 2010

Looking at the example code in the docs
http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
It does not compile on 2.1. The static fields DIRECTORY_PICTURES, DIRECTORY_MUSIC etc. Don't seem to be found. Using the filter api checkbox, I see that it was removed in Android 2.2 (or api version 8). So the manifest file needs to contain minSdk defined as 7.

View 1 Replies View Related

Android :: Programmatically Create / Destroy AppWidgets?

Mar 5, 2010

Is it possible to programmatically create and/or destroy AppWidgets?

View 7 Replies View Related

Android : Back Key Destroy An Activity

Jul 14, 2010

I have an activity defined as below:

CODE:............

A strange thing is that, when running on emulator, and the back key is pressed, the activity was destroyed (I saw onDestroy() called in log). But when running on my Nexus One phone, and the back key is pressed, the activity is not destroyed (I didn't see onDestroy() called in log).

View 1 Replies View Related

Android :: Can't Destroy GLSurfaceView For Exit Activity

Dec 2, 2009

what puzzle me so much is that i can't destroy a GLSurfaceView while i don't want to exit the activity.
the fact is when i destroy a GLSurfaceView which had show in the screen (that means it has binded to the activity's SurfaceHolder), the activity exits without any prompting. Perhaps ,the Context which provided in the Construction of GLSurfaceView joins the two things together. so my conclusion is that a GLSurfaceView can only be destroyed when exit the activity. is there anyway to destroy a GLSurfaceView without exit activity? is there someone can provide a clue?

View 2 Replies View Related

Android :: Activity Receiving Messages After On Destroy

Apr 28, 2010

I have an Activity with an inner Handler. The problem is that after the activity is destroyed, the Handler is receiving messages for the destroyed activity. This breaks things because the activity is in an inconsistent state. I'm thinking this might be a bug in Android - it should probably delete all the messages in the queue when the activity is destroyed. There doesn't appear to be any way I can manually delete all messages in the queue (except by calling removeMessage(int what) with every possible variation of what, which seems a bit ridiculous). The only other solution I can think of is to create my own is_destroyed instance variable and check it in handleMessage(), but again that seems like a ridiculous hack. Has anyone come across this problem before?

View 5 Replies View Related

Android :: Activity Objects In Memory After On Destroy

Aug 3, 2009

I have a activity MyActivity, which "kills" yourself using the finish () method. The problem is: after the kill operation, method onDestroy is called, but the object of type MyActivity is never garbage-collected (I forced the GC run). It is causing a memory leak, because MyActivity is launched many times, by other activities. Does anyone know when the Activity object is supposed to be garbage- collected, and what can be done to avoid the issue I mentioned?

View 2 Replies View Related

Android :: How To Not Destroy Activity When I Rotate Device

Jul 25, 2010

I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again.
How to not destroy the activity?

View 2 Replies View Related

Android :: Why Does Android Prefer Static Classes

Jun 24, 2010

I see a lot of Java code where android prefers to have developers use static inner classes. Particularly for patterns like the ViewHolder Pattern in custom ListAdapters. I'm not sure what the differences are between static and non-static classes. I've read about it but it doesn't seem to make sense when concerned with performance or memory-footprint.

View 5 Replies View Related

Android :: Activity Life Cycle

Nov 12, 2009

The main activity is running, then there is a interrupt. What was happended, onPause(), onStop() or other change of life cycle? 1. pressed the volume_down key and ringer volume toast occured, the life of main activity will not change? why? 2. short press the power key and the OS sleep,how about the activity ? 3. long press the power key and the phone options appear, how? 4. long press the home key and the tast list appear, how? 5. notifications curtain appear, how? 6. press the back key,how?

View 5 Replies View Related

Android :: Can't Grok Activity Life Cycle

Jun 6, 2010

I am having a really hard time grokking the Activity life cycle concept. The main issue is with onStop() and onDestory() not being guaranteed to be called before the process is killed. I though I had it figured out when I saw that the system calls onSaveInstanceState() when it's shutting down the activity to claim some memory. Thing is that the docs says onSaveInstanceState() will be called before onPause() but how does the system know at this point whether the activity will be killed by the system or the user?

Here is a use case:

1) My activity starts up and is running. 2) At some point I want to show a web page so I use an Intent to start an activity. 3) The web browser covers my app/activity so I would expect onPause() to be called followed by onStop(). At this point it's my understanding that all bets are off and I can be killed at any point without be called. 4) Since the system has enough memory onSaveInstance() doesn't get called. 5) The user presses home and decides to open some app which requires a lot of memory. 6) System wants more memory and decides to kill my app but I am already stopped (onPause() has been called).

View 1 Replies View Related

Android : Create A Library For Different Classes?

Jul 17, 2010

I'm new to JNI, i'm developing a native library for an Android project. I read some papers about JNI programming but i didn't understand if it is possible to create a library that can be loaded in different project classes with different packages. I read that to declare a new JNI method the syntax is:

the prefix "Java_"
an encoded fully qualified class name
an underscore ("_") separator
an encoded method name

Based on this definition it shouldn't be possible...

Suppose i had defined a class A in the package pkg1 with the native method foo contained in libfoo, and then i defined a class B in the package pkg2. Can i use libfoo and the foo method in B? How should i define the native method to achieve the result?

View 2 Replies View Related

Android : Avoiding An Activity To Destroy - Just Stopping Or Pausing It When Pushing The Back Button

Mar 19, 2010

I would like to pausing or putting the application on background when pressing the back button, I don't want the application to go through the destroy state. Things are when I override onKeyDown and when I force to pause or stop the application by using onPause, I have some issuees with the wakelock and application crash, but when I press home button I go through onPause method and I have no exception, it's weird!

View 1 Replies View Related

Android : Why Do We Create Inner Classes / Subclasses In Droid?

Oct 26, 2010

My Question is, why do we create inner classes or subclasses in android. Like the following example ...

View 13 Replies View Related

Android :: When Activity Closed Fields Not Released

May 20, 2010

In the following scenario, the fields of Activity is not released when the Activity is closed with calling finish().
Class MyClass {
}

View 3 Replies View Related

Android :: Activity Life Cycle - Serialization Taking Too Long

Jul 27, 2009

Finally after many days of getting StackOverflowError, I've tracked down the issue and fixed it, only to find that my game's serialization takes about 5 -10 seconds on the emulator and most likely around that in the target. So far my the lifecycle of my game is as follows

onCreate - check if serialization file exists, if it does, de- serializes it. (~ 5-10 seconds) onPause - if the game is not complete, then serialize it (~ 5-10 seconds) I remember reading somewhere that another activity's onResume will NOT get called UNTIL the previous activity's onPause has ended. So I am worried that my game is delaying another activity that wants to start from doing so i.e like a phone call etc. I think speeding up the serialization is not going to be feasible, so any ideas on what I can do? Can I serialize in the onDestroy instead of the onPause? I read that the onPause is the only safest place to store the state.

View 8 Replies View Related

Android :: Handle Activity Life Cycle Involving Sockets?

May 30, 2010

I have an Android activity which in turn starts a thread. In the thread I open a persistent TCP socket connection. When the socket connects to the server dynamic data is downloaded. The thread sends messages using Handler-class to the activity when data has been received. Now if the user happens to switch from portrait to landscape mode the activity gets an onDestroy call. At this moment I close the socket and stop the thread. When Android has switched landscape mode it calls onCreate yet again and I have to do a socket re-connect. Also, all of the data the activity received needs to be downloaded once more because the server does not have the ability to know what has been sent before, i.e. there is no "resume" feature.

Thus the problem is that there is alot of data which is resent all the time when landscape mode is changed. What are my options here? Should I create a service which handles the socket traffic towards the server thus I always got all the data which the server has sent in the service. Or should I disable landscape mode all together perhaps? Or would my best bet be to rewrite my server which is a VERY BIG job

View 1 Replies View Related

Android : Create Another Classes Object In A Class In Droid?

Jul 12, 2010

Is it possible to create another classes object in a class in android. code...

View 4 Replies View Related

Android :: Out Of Memory Error On Large Bitmaps And Activity Life Cycle

Jul 24, 2010

I have a scrollable map app which for now has a huge bitmap. It loads fine on startup, but when it looses foreground status and the user brings it backs again im getting an out of memory error. In onPause it trashes the bitmap using recycle, and marks it as null. The onResume checks to see if map==null and will load the bitmap back again, which is crashing the program despite me recycling the bitmap...Here are some bits of code. All of the other references to Bitmap map first check if it is null before loading/drawing..............

View 1 Replies View Related

Android :: Local Classes And Starting New Activity

Feb 14, 2009

I am having trouble running my application as every time i try to run it the simulator gives me a an error.

I looked through the Documentation but I couldn't find anything on user created classes.

CODE:.................

View 12 Replies View Related

Android :: Way To Create A C++ Static Lib To Use With Java On Droid?

Apr 27, 2010

I have some C++ code that I want to make into a static lib for use with Java on the Android platform. Can anyone point me to a resource that tells me how to do this? I am completely new to Java and Android.

View 2 Replies View Related

Android : Do Activity Classes Need To Be In A Separate .java File?

Sep 8, 2010

I am new to android development and am currently working my way through the "Hello..." Tutorials on the developer website.

I got stuck on the Tab Layout walkthrough and the only way I could resolve it was to put each Activity Class in a separate .java file.

I was wondering if all Activities need to be in separate .java files, or am I missing something.

View 1 Replies View Related

Android : How To Create Executable File With Linking Static Library

Feb 4, 2010

I have a static library: libhello.a and I want to create a executable file that link with this static library:Code...

View 3 Replies View Related

Android :: Some Rules Of Thumb For Naming Classes That Extend Activity Class?

Nov 5, 2010

I go back and forth about how to name activity classes.Activity seems to imply a verb, like EditContact, for example.But that seems more like what one would call the Intent that triggers EditContact.Should the activity be named ContactEditor instead?

View 2 Replies View Related

Android :: Where On .git.kernel.org Can Find Source For Classes As Activity Intent And ContentProvider

Sep 1, 2010

Not sure how to figure out which android project on android.git.kernel.org corresponds to the classes extended in some of the app examples in the SDK such as Activity, Intent, and ContentProvider.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved