First: This is for a project that is already deployed, and requires a solution that isn't "rewrite how the whole thing works"!

I have a "record detail" screen of sorts that is storing the corresponding RecordID in a static variable. Within this screen you can drill down, which passes the RecordID into a bundle and is retrieved by the new activity in order to display and edit the information appropriately. Going back to the original detail screen works as intended.

Now I must incorporate a feature that utilizes a new instance of this same detail screen; but if I spawn a new instance of this detail activity and pass on the RecordID and then go back to the first instance of the detail screen, that RecordID is now the same as the one that I passed to the second instance. This is due to the RecordID being static, I'm guessing.

When I make this RecordID no longer static, I can freely create a new instance of the detail class and go back and forth with the correct RecordIDs in each instance. However... this makes it so when you drill down into an activity within the detail and come back, the variables are now null.

What is the best way to fix this situation?

  • I don't think using savedInstanceState would work since the activity isn't actually going to be destroyed. I tried this and the method isn't even called.
  • I thought about doing startActivityForResult and just passing the record ID back to the detail screen, but that seems horrible inefficient.

2 Answers 2


You are correct in thinking that being static was causing the issue. Along with preventing a second instance's creation static reference means that there is no actual instantiation, you just reference the class itself. The reason that you are getting nulls when you back out is because you do not have a reference to the object above you.

Think of it like a tree navigation. If you start at a leaf and go up to the parent of the parent of the parent then you get to a point where you don't remember the way back and there are several possibilities. You need to store references up the chain. I would use a stack to hold the references as you move deeper. Then when you come back up then you can just pop the node off the stack.


Global (i.e., static) variables are considered evil in general, but that goes double on Android because of how the framework manages processes. The problem you described is an example of their general evilness. It gets weirder if the app goes into the background and the process is destroyed. In that case, the same activity (conceptually... actually a different instance) will find your static variable null, even though you never navigated away from it. And it gets weirder still if you "exit" the app and the framework decides to keep the process. In that case, your app will see the old value the next time it starts.

startActivityForResult/onActivityResult is the preferred way to pass values up and down the activity stack. It's no more inefficient than anything else in Android, and it's not like it happens thousands of times in a loop or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.