Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android strong reference

Tags:

object

android

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 ??

like image 999
d-man Avatar asked Jan 15 '10 11:01

d-man


People also ask

What is strong reference in Android?

Strong References: This is the default type/class of Reference Object. Any object which has an active strong reference are not eligible for garbage collection. The object is garbage collected only when the variable which was strongly referenced points to null.

What is weak reference in Android?

Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings. Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable.

What is weak reference and strong reference?

The garbage collector cannot collect an object in use by an application while the application's code can reach that object. The application is said to have a strong reference to the object. A weak reference permits the garbage collector to collect the object while still allowing the application to access the object.

How does a weak reference work?

A weakly referenced object is cleared by the Garbage Collector when it's weakly reachable. Weak reachability means that an object has neither strong nor soft references pointing to it. The object can be reached only by traversing a weak reference.


3 Answers

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.

like image 187
Matthias Avatar answered Oct 05 '22 11:10

Matthias


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.

like image 38
Dave Webb Avatar answered Oct 05 '22 10:10

Dave Webb


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);
    }
like image 37
d-man Avatar answered Oct 05 '22 10:10

d-man