Android onCreateの詳細

10050 ワード

AndroidManifestでxmlファイルのintent-filter要素には、次の2つの文があります.
<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        intent-filter>
activity>

書いたアプリが携帯電話に投稿されると、「引き出し」でアプリケーションのアイコンをダブルクリックすると、このクリックイベントが2つのパラメータを含むIntentにパッケージされます.上記の2つのパラメータaction、categoryがアプリケーションに渡された後、アプリケーションの機能リストファイルでその意図に一致する意図フィルタを探します.一致に成功すれば、一致する意図フィルタが存在するActivity要素を見つけ、activity要素の「name」に従います.「属性は、対応するActivityクラスを検索します.次に、AndroidオペレーティングシステムがActivityクラスのインスタンスオブジェクトを作成します.オブジェクトの作成が完了すると、親のActivityのonCreateメソッドを書き換えるonCreateメソッドが実行されます.onCreateメソッドは、Activityインスタンスオブジェクトを初期化するために使用されます.次のように、Android UIdemo.javaクラスのonCreateメソッドのコードです.
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caculator_layout);

        Log.d(TAG,"debug");
        Log.i(TAG,"info");
        Log.w(TAG,"warm");
        Log.e(TAG,"error");
        Log.v(TAG,"verbose");

        //   
        initView();

        //      
        initClickEvent();

    }

一、super.onCreate(savedInstanceState)
そのうちsuper.onCreate(savedInstancesState)の役割は、親ActivityのonCreateメソッドを呼び出してインタフェースの描画を実現することです.自分で定義したActivityサブクラスのonCreateメソッドを実装するときは、インタフェースを描画できるように呼び出すことを忘れないでください.
super.onCreateは主にいくつかのコンポーネントをロードします.
最初のことはsuperですonCreate(savedInstanceState)ですが、この文はサブクラスのonCreateメソッドの任意の場所に置くことができます.問題はsuperだけです.onCreate(savedInstancesState)は実行されなければならないので、最初の行に置いたほうが明確に見えます.
TargetActivityではonCreateメソッドが書き換えられているが、ActivityのonCreateメソッドではサブクラスにsuperがない場合、いくつかの基本的な基礎が操作されている.onCreateメソッドの呼び出しにより、サブクラスのonCreateメソッドの機能が不完全になり、エラーが発生します.
savedInstanceState
activityのライフサイクルでは、可視段階を離れたり、焦点を失ったりすれば、activityはプロセスによって終了する可能性が高い!、KILLに落とされた場合、その時の状態を保存できる仕組みが必要です.これがsavedInstancesStateの役割です.ActivityがPAUSEにある場合、killの前にonSaveInstancesState()を呼び出して現在のactivityのステータス情報(pausedステータスの場合、KILLEDになる場合)を保存することができます.状態情報を保存するBundleは同時に2つのmethod、すなわちonRestoreInstancesState()and onCreate()に伝達される.
二、setContentView(R.layout.caculator_layout)
≪アクション|Actions|emdw≫:インタフェースをロードします.このメソッドに入力されるパラメータは「R.layout.caculator_Layout」は、R.javaクラスの静的内部クラスlayoutの静的定数caculator_layoutの値を意味し、resディレクトリの下にあるlayoutサブディレクトリの下にあるcaculator_layout.xmlファイルを指す識別子である.したがって、caculator_layout.xmlが定義する図面を表示することを意味する.
三、二種類のクリックイベント
 /*
    *      
 */
    private void initClickEvent(){
        //       
        mCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                Log.d(TAG, "content=="+((TextView)v).getText() );
            }
        });

        //       
        mPlusOrMinus.setOnClickListener(this);
        mPlus.setOnClickListener(this);
    }

 @Override
    public void onClick(View v) {
        //             ,            ,     
        //     
        if(v == mPlusOrMinus){
            Log.d(TAG, "   mPlusOrMinus" + ((TextView)v).getText().toString());
        }else if(v == mPlus){
            //        
        }

        //        switch   id

        //   id
        int id = v.getId();
        switch(id){
            case R.id.tv_number_one:
                //one        ,      

                break;
            case R.id.tv_equal:
                //      
                break;
        }

    }

コントロールの初期化
  /*
    *       
     */
    private  void initView()
    {
        mCancel = (TextView) this.findViewById(R.id.tv_cancel);
        mPlusOrMinus = (TextView) this.findViewById(R.id.tv_plus_or_minus);
        mMod = (TextView) this.findViewById(R.id.tv_mod);
        mDivider = (TextView) this.findViewById(R.id.tv_divider);
        mOne = (TextView) this.findViewById(R.id.tv_number_one);
        mTwo = (TextView) this.findViewById(R.id.tv_number_two);
        mThree = (TextView) this.findViewById(R.id.tv_number_three);
        mTime = (TextView) this.findViewById(R.id.tv_time);
        mFour = (TextView) this.findViewById(R.id.tv_number_four);
        mFive = (TextView) this.findViewById(R.id.tv_number_five);
        mSix = (TextView) this.findViewById(R.id.tv_number_six);
        mMinus = (TextView) this.findViewById(R.id.tv_minus);
        mSeven = (TextView) this.findViewById(R.id.tv_number_seven);
        mEight = (TextView) this.findViewById(R.id.tv_number_eight);
        mNine = (TextView) this.findViewById(R.id.tv_number_nine);
        mPlus = (TextView) this.findViewById(R.id.tv_plus);
        mZero = (TextView) this.findViewById(R.id.tv_number_zero);
    }