1

I am trying to implement a Android library. And in the tutorial, I found the below text and I could not understand why when I have a non-static inner class I can not reference the App or the Activity Context?

Can any one explain it to me please?

Another common tip is to avoid referencing the context in non-static inner classes in activities when you’re not in control over their life cycle. Use static inner classes and weak references to the activity instead.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • You can reference the app context but it is a good practice to avoid it in the situation when you are not in control over the application live cycle. When you are not in control of the app live cycle the reference to app context may change and your reference to app context will be not updated. So when you use a weak reference then the reference will be removed after GC and you know that you have to obtain a new one. – Lubo Oct 31 '14 at 15:54
  • http://stackoverflow.com/questions/1353309/java-static-vs-non-static-inner-class – Pedro Oliveira Oct 31 '14 at 15:55
  • @Lubo thank you. but what us GC and "the weak reference" i do not know them. – Amrmsmb Nov 01 '14 at 08:53
  • GC means Garbage Collection. In java it is invoked automatically or if you call System.gc(). It will search objects in memory that are not referenced anymore and then remove them. So it frees memory for another purposes. If a object is referenced via strong reference (Object o = new Object(); then the "o" is a reference to an object) then the object is not cleared by GC because you have a reference to it. But if you use a weak reference (reference stored in class WeakReference) then the GC will remove the object. – Lubo Nov 01 '14 at 18:54

2 Answers2

1

I think its a way to prevent memory leaks.

Example:

Consider an AsyncTask running inside an activity that takes a lot of time to complete:

public class VeryLongRunningTask extends AsyncTask<?,?,?> {

    protected Object doInBackground(Object[] params) {
        while(runForVeryLongTime()){
            doSomethingWithContext(getContext())
        }
        return null;
    }
}

Now on Your activity's onCreate you run the task

public void onCreate(Bundle bundle){
    new VeryLongRunningTask().execute()
}

As you can see VeryLongRunningTask will hold a reference of your activity for a very long time (probably much longer that the Activity will be visible). This means that the activity will not be garbage collected for a ver long time.

fernandohur
  • 7,014
  • 11
  • 48
  • 86
0

The system maintains only one instance of a static class and so if the inner class is static, there will be only one instance of it per activity. If the inner class isn't static then you can have multiple instances on the inner class accessing the activity and each would have the ability to modify the activity state independently.

This can cause numerous challenges if it is that you actually create multiple instances of an inner class. Think of it this way, you have an inner class that updates a counter variable in the activity and then loops through a list based on the value of this counter. If the inner class is static, then there is just one instance of it doing this. If it is not static however, then there could be, say, three instances doing this. Remember all three are accessing the same activity and the same counter, so if they run at different times (very likely) they would update the variable at different times and then loop. So when the first instance updates the variable and begins its loop checking the value as it goes, the second instance then changes the variable and now affects the loop in the first. It then starts its loop and then the third instance updates the counter again affecting both other instances. So now none of the previously running instances on the inner class will produce the correct result.

This is very simplistic example and things can get much, much worse.

dibble
  • 299
  • 3
  • 3
  • This information is not correct. only an nested class can be static. But you can have multiple instances of a nested static class. The difference between static and non-static nested classes is that the non-static class has an implicit reference to the outer class. – cyroxis Jun 01 '15 at 20:09