Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Bob Kerns » Tue, 20 Apr 2010 17:18:58 GMT


Sponsored Links
 I do this, without any issues at all.

How are you getting your Application instance from your activities?
not getApplicationContext() I hope...

Have you examined the state from the debugger? There may be something
you're missing about the state of things.



> thread



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by patbenatar » Tue, 20 Apr 2010 18:04:18 GMT


 Bob-

Nope, I'm using getApplication()

For instance [inside an Activity]:


MyApplication application;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    application = (MyApplication) this.getApplication();
}


The error is most easily re-produceable when I am in Activity B, after
A started it for result, and then click a button in B to launch Maps
with an Intent to search for an address, then from Maps I hit Home
then my launcher icon on my Home Screen to go back into my app--BAM!
Activity B fails onResume with a NullPointerException coming from a
getter method on my MyApplication class, when I know for sure Activity
A properly set the data in MyApplication..

So strange...

Thanks,
Nick






> > thread


Sponsored Links


Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by westmeadboy » Tue, 20 Apr 2010 19:05:50 GMT


 Not sure if this helps, but use
 http://developer.android.com/reference/java/lang/System.html #identityHashCode(java.lang.Object)
to check whether the Application object is indeed the same in both
cases (or whether they are in different DVMs)





> > > thread



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Mark Murphy » Tue, 20 Apr 2010 19:47:52 GMT


 


A database, flat file, or other persistent store is the proper place for
it, perhaps cached via a custom Application class or static data members.

> 



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by String » Tue, 20 Apr 2010 20:59:03 GMT


 



Sure, that's the normal app lifecycle. But if this is the case, why
isn't the Activity's onCreate being called? To me, that's the
confusing part of this.

String

--



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Mark Murphy » Tue, 20 Apr 2010 21:04:00 GMT


 




Whoops. I missed that part. If that's happening, that is very strange.
The Application object should be the second-to-last thing to go -- if
activities are in RAM, so should the Application, AFAIK.

That being said, the *last* thing to go is static data members, since
they'll only get cleaned up when the whole Dalvik VM is reset or the
process is fully terminated. It may be worth a test to see if a static
data member survives all of this.

Something definitely is not adding up.

My apologies for missing that part of the original post and not
addressing it in my earlier reply.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Android Programming Tutorials_ Version 2.0 Available!

--



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Christopher » Wed, 21 Apr 2010 00:48:53 GMT


 Just out of curiosity, is your Activity making use of onNewIntent()?
I was curious because I recently worked on an Activity that did, and
found that many variables in the Activity were set to null after this
method completed.  It's almost as if onNewIntent does a quick reset of
the instance, which given the nature of onNewIntent could make sense.
I might not be 100% accurate in terms of what's going on, but thought
perhaps the information could help.


(Note: I think I originally replied only to the author, so I attempted
to reconstruct the post as best I could.  I apologize if suddenly
there are two almost identical posts from me that show up).


Christopher

--



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by patbenatar » Wed, 21 Apr 2010 09:36:41 GMT


 Thanks for all the input guys--

Mark, it is indeed very strange that my Activity's onCreate is not
being called here. And the reason I have chosen not to use a database
or flat file is because the lifetime of this data is a maximum of 20
minutes before my app rules it outdated and fetches new data from the
server. Didn't really see the point in writing data with such a short
shelf life to a more permanent data store. If my app is destroyed and
relaunched within this 20 minute period, I have no problem with doing
a fresh API fetch (why not? the more frequently [while remaining un-
invasive] I can update the data, the better)... I will throw

Christopher, nope not overring onNewIntent--sounds like some weird
behavior you've run into there...

Is it possible that a memory leak could possibly be the cause of this
behavior? I had some leaks previously but went through all my code a
couple weeks ago and believe I have removed them all but I haven't
completely confirmed this [although my app hasn't hit any OutOfMemory
errors since then]. I don't see why a memory leak would be removing
data though, normally they causes data to pile up...

I have found one issue in my code that may resolve this.. I
implemented a potential fix but have not had a chance to test on my
device yet. Will post back. In the meanwhile, anyone else with
thoughts on this matter, please share :)

Nick





>



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Dianne Hackborn » Wed, 21 Apr 2010 11:23:50 GMT


 Fwiw, there is definitely no clearing of instance variables when
onNewIntent() is called.  I can't imagine how the framework would even clear
instance variables, except for using reflection to find them and poke them,
which we definitely don't do.

For these situations it may be useful to look at the event log (adb logcat
-b events) to see what the system is doing, and also possibly the activity
manager state (adb shell dumpsys activity) to see the state the system has
your activities in.




>



Weird: It appears as if sometimes my Activity will be resumed but previously set instance variables are null

by Bob Kerns » Thu, 22 Apr 2010 03:59:39 GMT


 Or use the debugger, and set breakpoints in each method on the
affected class, and step through until you see where the field is
being cleared.




> >



Other Threads

1. Threading in activity and AsyncTask problem

i have a class derived from AsyncTask and i initialize it in my
activity. Now i m facing two problems...

1. I want to send result back to my activity and execute a chunk of
code in my activty on onPostExecute(Object result) event. (i tried
handler but its not working)
2. I want to execute asynctask multiple times.

Please suggest me some better solution or a fix to this...
thanx in advance

ACTIVITY CODE:

                         StudentTask st = new StudentTask(getThis());
                         st.execute(new Object[] {StudentTask.Task.AUTH, null, 
userName,
password);


CLASS CODE:

public class StudentTask extends AsyncTask {

        private Context context;
        private ProgressDialog pd;

public static enum Task {
                SYNCRONIZE, AUTH, ACTIVATE
        }

public StudentTask(Context context) {
                super();
                this.context = context;
        }

        @Override
        protected void onPreExecute() {
                pd = ProgressDialog.show(context, "Please wait", "Starting");
        }

        @Override
        protected Object doInBackground(Object... params) {

                Object result = null;
                Task t = (Task) params[0];
                switch (t) {
                case AUTH:
                        if (this.authenticateStudent(params[1].toString(),
                                        params[2].toString()).IsSuccessFul()) {
                                result = true;
                        } else {
                                result = false;
                        }
                        break;
                case SYNCRONIZE:
result =                        this.synchronize(null, null);
                        break;
                default:
                        break;
                }
                return result;
        }

        @Override
        protected void onProgressUpdate(Object... values) {
                pd.setMessage(values[0].toString());
                super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Object result) {
                pd.dismiss();
                // Send this result back to activity and execute some code there
e.g. launch next activity or display error
                super.onPostExecute(result);
        }

-- 

2. Android Icon Templates Pack - Menu Icons PSD Corrupted?

Hello everyone,

Opening the menu icon template file from the Android Icon Templates
Pack just shows two icon shapes in Photoshop CS2 on my PC, but there
should be many more. I assume the file is corrupted or I still don't
have the specifically needed version of Photoshop.

Can anyone please provide a complete and compatible version of the
Menu_Icons_master.psd? It would be sufficient to have all the basic
Android menu icon shapes as .pdf files. From the .psd i can still read
the gradients styles and so on.


A few more details what I have tried so far:
I just downloaded the Android Icon Templates Pack v1.0 mentioned on
the site:

http://developer.android.com/guide/practices/ui_guidelines/icon_design.html#templatespack

Extracting the zip, the .psd file Menu_Icons_master.psd can be found
in the menu folder. Opening it in my Photoshop CS2 first brought an
error message that DroidSans font could not be found - I solved this
issue by installing the DroidSans font from:

http://damieng.com/blog/2007/11/14/droid-font-family-courtesy-of-google-ascender

Restarting Photoshop and opening the file now brings no error message,
but only the icon shapes for a house and a star are shown, missing all
other menu icon shapes.


Many thanks for your help!
Dirk

3. Remove/Replace Google Setup Wizard

4. OpenSSLSessionImpl.java finalize() problem - HeapWorker is wedged

5. java.io.File.DeleteOnExit holds too many strings

6. NDK Development with Objective-C 2.0

7. why isn't the progress bar full width of the view