Skip to content Skip to sidebar Skip to footer

Android: Is There A Way Around SuperNotCalledException?

I'm trying to override a class derived from Activity (called NativeActivity) so I can set my own content view created in Java while leaving the rest of its functionality in tact.

Solution 1:

You must call super.onCreate(savedInstance) when you override this method on an Activity.

Digging through the code this check looks like it is intended to make sure that any custom Instrumentation works correctly. And the requirement of calling super on some methods in all our custom Activities was an unintended consequence of the implementation.

Without getting into the details of why this is a bad OOP design, I can say that at least it's simple enough to just call super.onCreate(savedInstanceState) in your onCreate() method. As you can see from the code, there are no bad side effects.


Solution 2:

One thing I figured out from studying the sourcecode for Activity, is you could call one of the methods that set mCalled to true in the base Activity clase, which are not overridden in the subclass, and which do nothing in the base Activity class. So in the case of NativeActivty, one could call something like super.onRestart();, because this class is not overridden by NativeActivity, and as you can see below, it doesn't do anything in the Activity class:

protected void onRestart() {
    mCalled = true;
}

This is quite hackish, and could be broken at some future version, but can be a quick solution if you don't want to have to recreate an entire subclass of Activity just because of one simple boolean.


Post a Comment for "Android: Is There A Way Around SuperNotCalledException?"