3

I extend a "Login" class from the MainActivity.

MainActivity looks like this:

 public class MainActivity extends FragmentActivity {
/** Called when the activity is first created. */
private static String TAG = "MainActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    Intent login_activity = new Intent(this, Login.class);
    startActivity(login_activity);
    Log.d(TAG,"Login created");
}
}

In the login.class I am just calling the layout:

public class Login extends MainActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    //super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
     }
}

You can see, that I commented out the "super.oncreate" in the login class. It doesn't make sense to call the MainActivitys oncreate here again. But whith this outcommented super.oncreate I'll get some Exception:

12-31 11:37:47.688: E/AndroidRuntime(4206): FATAL EXCEPTION: main
12-31 11:37:47.688: E/AndroidRuntime(4206): android.app.SuperNotCalledException: Activity {de.svennergr.htn/de.svennergr.htn.Login} did not call through to super.onCreate()
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.os.Looper.loop(Looper.java:137)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at java.lang.reflect.Method.invokeNative(Native Method)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at java.lang.reflect.Method.invoke(Method.java:511)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 11:37:47.688: E/AndroidRuntime(4206):     at dalvik.system.NativeStart.main(Native Method)

When I do not comment super.oncreate, I'll get an not stopping loop creating lots of "Login" objects/activities.

How do I solve this?

svennergr
  • 800
  • 1
  • 8
  • 26

4 Answers4

4

In Android, you must adhere to the life cycle of the Activities. Each time you override a life cycle method, you must call super to ensure consistency with Activities life cycle.

The point here is not to call MainActivity.onCreate but Activity.onCreate. If you don't want to inherit from the behavior of MainActivity inside LoginActivity, then don't extend from it. Maybe you need an intermediate parent common parent class to group only the desired behavior.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
2

You must always explicitly call onCreate() superclass method when subclassing Activity so the system could properly initialize it so you can't just eliminate the call to superclass onCreate().

It's generally not a very good idea to start an activity in another activity's onCreate() method. Just think about other ways of launching your Login activity (make a button that launches it e.g.).

If you are planning to have multiple subclasses of your MainActivity just incapsulate the common behavior and components in it and launch your subclasses (Login activity) directly. Or if you want the Login activity appear in the beginning you can make it a LAUNCHER activity in AndroidManifest.xml.

Please, read more about activity lifecycles and study some theory before development.

Taras Leskiv
  • 1,835
  • 15
  • 33
  • 1
    We are not talking about constructors here. Ok, onCreate is the first method that is called in the Activities life cycle but it's not a constructor. – Snicolas Dec 31 '12 at 13:55
  • sorry, you are absolutely right, edited my answer, I meant invoking super.onCreate() method. – Taras Leskiv Dec 31 '12 at 13:58
0

the rule is, if you override onCreate() you must call the super.onCreate()...what you can do is to setContentView() on the MainActivity like this: setContentView(getCurrentViewLayoutId()) and the getCurrentViewLayoutId() would be a method that the LoginActivity.java would implement...like this you can remove the onCreate() implementation completely from the LoginActivity.java..then you won't have any problems with "super.onCreate() check". But it's probably a good idea to do this startActivity call in the onResume().

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
0

Is there any specific reason of extending MainActivity for Login?

Since you are calling Login intent in MainActivity and Login need to call super.onCreate() it's going to infinite loop. So, I think it's better to extend Activity instead of MainActivity.

Kameswari
  • 738
  • 7
  • 27