Android Activity which overridden functions must call super.*

左心房为你撑大大i 提交于 2019-11-30 03:54:36

must:

  • onCreate(Bundle savedInstanceState);
  • onStart();
  • onRestart();
  • onResume();
  • onPause();
  • onStop();
  • onDestroy();
  • onPostCreate(Bundle savedInstanceState);
  • onPostResume();

should / shouldn't: (may be helpful to call superclass method, unless you manage activity's state or deliberately alter activity's behavior yourself - in such case it may be harmful)

  • onSaveInstanceState(Bundle savedInstanceState);
  • onRestoreInstanceState(Bundle savedInstanceState);
  • onBackPressed();
  • onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
  • onCreateOptionsMenu(Menu menu);
  • onOptionsItemSelected(MenuItem item);
  • onContextItemSelected(MenuItem item);
  • onMenuItemSelected(int featureId, MenuItem item);

Actually, when overriding any method, it's a good practice to look at the source code of the overridden class and see what exactly this method does.

Its just a good practice to call the super implementation.

BUT some times its mandatory, like in "onCreate()", else there will be an "ActivityInstantiateingexcepetion"in this case.

Also, some times you want to override the super implementation in some cases, like

onBackPressed(){
if(myFlag){
// do my stuff
}
else {
// Do usual stuff on Back pressed
super.onBackPressed ();
}

So this way if your "myFlag" will be true, your stuff will be done else normal onBackpressed will be executed.

Its all Activity class method.In Java and Android if you call superclass method ,You must write super.methodName

all these methods should call super.method() I think

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!