Android strong reference
I am having SearchCritiera object and i make it singleton and declare this variable as static, now problem is if i left my application remain open for couple of hours that static object is removed by Android OS, how can i make sure static object should not be removed by the OS.
like i know there are few keywords like
Weekreference and softreference is there any strongreference keyword which can tell Android OS do not remove the reference ??
Asked by: John960 | Posted: 24-01-2022
Answer 1
Don't use static references, even if your application remains in the foreground, these objects may be destroyed by the garbage collector (I've seen this happening a couple times now).
You can simply avoid this by storing them in your unique Application instance. That object is guaranteed to live as long as your app.
Answered by: Carlos272 | Posted: 25-02-2022Answer 2
Sadly, you can't force Android to keep your application in memory. If the OS feels it needs more memory for a foreground application or service it reserves the right to terminate one or all of your Activities.
I'm assuming what's happening is your static object is being lost and re-created when you call it, meaning that it has lost its state. That said, if you have a reference to the object in a foreground Activity I'm a little surprised that it's getting lost.
The best you can do work around this is hook into the lifecycle events and save the state of your singleton object and then restore it when appropriate.
Unfortunately, there are no Application wide lifecycle events in Androd. Have a look at this question for how to save transient application state.
Answered by: Vanessa351 | Posted: 25-02-2022Answer 3
If i am not wrong when application remain open for long time data is released by the android OS, and while stopiong activity it will call "onSaveInstanceState" and when can i store searchritiera into this method and will it retrive back when "onRestoreInstanceState" is get called ?
private static SearchCriteria searchCriteria;
@Override
public void onSaveInstanceState(Bundle outState)
{
outState.putSerializable(WLConstants.SEARCH_CRITERIA, searchCriteria);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if(savedInstanceState != null){
searchCriteria = (SearchCriteria)
savedInstanceState.getSerializable(WLConstants.SEARCH_CRITERIA);
}
super.onRestoreInstanceState(savedInstanceState);
}
Answered by: Victoria352 | Posted: 25-02-2022
Similar questions
java - Is there an Android XML reference?
This question already has answers here:
android - Reference to XML file is not a member of the R file
How can I had to class layout in R another XML file?
It should be automatic as I had new resources to res, but it's not.
Someone knows what I did wrong?
I open an activity and now I want to open another activity that will work with another XML
example.
I have menu and main.xml.
Now I want to go for another activity called gamescreen using this method:
newGameButton.setOnClickListener(new OnC...
Adding a project reference to an android project
I am a C# developer and getting started with Android. I am attempting to duplicate a couple applications I already have in VS. The project is made of 2 executables and 1 common assembly. The 2 executables contain the application specific logic while the common contains centralized forms and logic (such as login form). I am using Eclipse. So how can I accomplish this layout?
Thanks
java - Is there an Android XML reference?
This question already has answers here:
android - Getting Reference to Calling Activity from AsyncTask (NOT as an inner class)
Is it at all possible, from within an AsyncTask that is NOT an inner class of the calling Activity class, to get a reference to the instance of Activity that initiated execution of the AsyncTask?
I am aware of this thread, however it doesn't exactly address how to reference the calling Activity. Some suggest passing a ref...
How can I reference a drawable class in Android XML
I've created a class that extends drawable that I'd like to reference inside a resource xml. I happen to need it in a selector, like so:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_pressed="false"
android:drawable="com.sample.android.contacts.TopBarCollapsed"
/>
<item android:state_window_focused="true" andro...
android - How do I reference sqlite db column to use in update statement
I am trying to update a datetime column in an android sqlite db to use international date format (yyyy-mm-dd) instead of the current format (mm/dd/yyyy). I want to use the sqlite date() function to reformat the current value of the column. I thought it would be as simple as the following:
update tblename set thedate = date(thedate)
but the above does not work.
How woul...
android - Reference to XML file is not a member of the R file
How can I had to class layout in R another XML file?
It should be automatic as I had new resources to res, but it's not.
Someone knows what I did wrong?
I open an activity and now I want to open another activity that will work with another XML
example.
I have menu and main.xml.
Now I want to go for another activity called gamescreen using this method:
newGameButton.setOnClickListener(new OnC...
Android SDK - Reference the phone's gallery app?
As of right now, in my app I have created a rudimentary gallery app using the provided widget, I need this to select a picture from the phone. This is working fine and everything, but lacking very much in presentation.
I've got a couple apps on my phone that do the same thing, but they somehow use the gallery that's already in the phone to let the user select an image. FourSquare, for example, when you select an im...
java - Pass reference to another activity - Android
I am very new to android, so I am experimenting with the tutorial code. In particular, the notepad.
If you look here: http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html, at Step 5 they are putting values so that it can be used by NoteEdit.
I would like to...
java - How do I reference adk resource in a different Android project?
I am developing a study project using the mosembro (mobile semantic browser) project inside my Android project. I am using Eclipse and I created my project, downloaded / imported mosembro and included in my project's build path.
So now my code needs to refer to a resource in the mosembro's project. I want to do something like this:
String commonJS = getScript(R.raw.common);
In th...
android - ListView I can't reference in R.id.list?
ListView ls=(ListView)findViewById(**R.id.list**);
ls.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(ListAllTracks.this);
adb.setTitle("LVSelectedItemExample");
// adb.setMessage("Selected Item is = "+String.valueOf(ls.getItemIdAtPosition(position)));
ad...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android