Android的活动而重写功能必须调用超。* [英] Android Activity which overridden functions must call super.*

查看:604
本文介绍了Android的活动而重写功能必须调用超。*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建自己的活动的子类,我们覆盖的一些基本活动的生命周期功能。

When creating own Activity subclass, we are overriding some of the basic Activity lifecycle functions.

在这其中,我们的必须调用super的实现,我们的应该和它在哪里的唯一的好方式

In which of these we must call super implementation, where we should and where is it only good manner ?

// base lifecycle
onCreate(Bundle savedInstanceState);
onStart();
onRestart();
onResume();
onPause();
onStop();
onDestroy();
finalize();
onUserLeaveHint();
// instance state
onSaveInstanceState(Bundle outState);
onRestoreInstanceState(Bundle savedInstanceState)
// others
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
onCreateOptionsMenu(Menu menu);
onOptionsItemSelected(MenuItem item);
onContextItemSelected(MenuItem item);
onMenuItemSelected(int featureId, MenuItem item);
onActivityResult(int requestCode, int resultCode, Intent data);
onBackPressed();

的方法列表不是终点,随意添加更多的功能,我们应该知道,该怎么做。

对于那些谁感兴趣的是,在这里应该是 super.methodName 放在
<一href="http://stackoverflow.com/questions/4994959/locations-of-super-calls-in-android-eclipse-plugin-generated-$c$c-reliable">Locations超()在Android的Eclipse插件调用生成code可靠吗?

For those who are interested in, where should be super.methodName placed
Locations of super() calls in Android Eclipse Plugin generated code reliable?

推荐答案

必须

  • 的onCreate(束savedInstanceState);
  • ONSTART();
  • onRestart();
  • onResume();
  • 的onPause();
  • 的onStop();
  • 的onDestroy();
  • onPostCreate(包savedInstanceState);
  • onPostResume();
  • onCreate(Bundle savedInstanceState);
  • onStart();
  • onRestart();
  • onResume();
  • onPause();
  • onStop();
  • onDestroy();
  • onPostCreate(Bundle savedInstanceState);
  • onPostResume();

应该:

  • onCreateContextMenu(文本菜单菜单,视图V,ContextMenuInfo menuInfo);
  • onCreateOptionsMenu(菜单菜单);
  • onOptionsItemSelected(菜单项项);
  • onContextItemSelected(菜单项项);
  • onMenuItemSelected(INT FEATUREID,菜单项项);
  • onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
  • onCreateOptionsMenu(Menu menu);
  • onOptionsItemSelected(MenuItem item);
  • onContextItemSelected(MenuItem item);
  • onMenuItemSelected(int featureId, MenuItem item);

应该/不: (可能是有益的调用父类的方法,除非你管理活动状态自己 - 在这种情况下,它可能是有害的)

should/shouldn't: (may be helpfull to call superclass method, unless you manage activity state yourself - in such case it may be harmful)

  • 的onSaveInstanceState(包savedInstanceState);
  • onRestoreInstanceState(包savedInstanceState);
  • onBack pressed();
  • onSaveInstanceState(Bundle savedInstanceState);
  • onRestoreInstanceState(Bundle savedInstanceState);
  • onBackPressed();

其实,覆盖任何方法时,这是一个很好的做法,看看覆盖类的源$ C ​​$ C,看看究竟这种方法做。

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.

这篇关于Android的活动而重写功能必须调用超。*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆