android的activity

1.簡單介紹

  應用程序組件,提供交互界面。android

2.Activity的建立

  1. 繼承Activity類
  2. 重寫方法
  3. 顯示佈局
  4. 在AndroidManifest中註冊Activity

 

3.生命週期

  1. onCreate()
  2. onStart()
  3. onResume()
  4. Running
  5. onPause()
  6. onStop()
  7. onDestroy()

4.代碼實例

package com.example.activity;

import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {
    final String TAG = "tag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "1");
    }
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.i(TAG, "2");
    }
    
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.i(TAG, "3");
    }
    
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.i(TAG, "4");
    }
    
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.i(TAG, "5");
    }
    
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i(TAG, "6");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
相關文章
相關標籤/搜索