search

Home  >  Q&A  >  body text

android中子activity是否必须重写oncreate()

子activity是否必须重写oncreate(),如果不是必须的话,子activity默认调用该方法的原理是什么。

父类Activity:

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    protected Context mContext;
    private ConnectivityManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 锁定竖屏
        mContext = getActivityContext();
        initView();
        ButterKnife.bind(this);
        initdata();
        MyApplication.getInstance().addActivity(this);
    }
    /**
     * 初始activity方法
     */
    private void initView() {
        loadViewLayout();
    }
    private void initdata(){
        findViewById();
        setListener();
        processLogic();
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
//        StatService.onPause(mContext);
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
//        StatService.onResume(mContext);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        MyApplication.getInstance().finishActivity(this);
    }
    /**
     * 加载页面layout
     */
    protected abstract void loadViewLayout();

    /**
     * 加载页面元素
     */
    protected abstract void findViewById();

    /**
     * 设置各种事件的监听器
     */
    protected abstract void setListener();

    /**
     * 业务逻辑处理,主要与后端交互
     */
    protected abstract void processLogic();


    /**
     * Activity.this
     */
    protected abstract Context getActivityContext();

子类 Activity:

public class WelcomeActivity extends BaseActivity {

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_welcome);
    }

    @Override
    protected void findViewById() {

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(WelcomeActivity.this,HomeTabActivity.class));
                finish();
            }
        }, 2000);
    }

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    public void onClick(View view) {

    }
}
大家讲道理大家讲道理2772 days ago712

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 09:08:34

    In object-oriented, subclasses will inherit the behavioral attributes of the parent class. Therefore, the subclass does not need to override the onCreate(...) function.

    With all due respect, I recommend two good books to you:

    1. O'Reilly: Head First Java (Chinese version 2nd edition covers Java5.0)

    2. Java Programming Thoughts (4th Edition) (Thinking in Java)

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:08:34

    Judging from the source code you shared, oncrate is not abstracted out in your parent class, BaseActivity, so of course it does not need to be called, but subclasses can also rewrite it. As for subActivities, this method will be called by default. The principle is even simpler:
    When the Java virtual machine executes a program, the parent class will be executed first, so the Oncreate method of the parent class will be executed at the beginning, and then the abstracted methods in the child class will not be abstracted in the parent class. Called, so when executing to the subclass, these methods are executed!

    reply
    0
  • Cancelreply