Activity

来自个人维基
跳转至: 导航搜索

目录

Activity

media:Android中activity启动流程分析.pdf‎

介绍

Activity是Android中基本的用户接口,用户界面都是以Activity提供在系统中的.在这里主要描述的是Activity的使用方法和一些特性,关于更深入的内容请参考Android组件

生命周期

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        view = new Dnview(this);
 
       // setContentView(view);
        setContentView(R.layout.main);
    }
 
    @Override
    public void onPause()
    {
    	super.onPause();
    	Log.d(TAG, "onPause");
    }
    @Override
    public void onRestart()
    {
    	super.onRestart();
    	Log.d(TAG, "onRestart");
    }
    @Override
    public void onStart()
    {
    	super.onStart();
    	Log.d(TAG, "onStart");
    }
 
    @Override
    public void onDestroy()
    {
    	super.onDestroy();
    	Log.d(TAG, "onDestroy");
    }
 
    @Override
    public void onResume()
    {
    	super.onResume();
    	Log.d(TAG, "onResume");
    }
    @Override
    public void onStop()
    {
    	super.onStop();
    	Log.d(TAG,"onStop");
    }

显示

在Activity的onCreate函数中,需要调用函数setContentView给Activity提供一个用于显示的View.这个View可以是xml格式的layout,也可以是用户自己从View类继承出来的自己的View。
View类中onDraw函数会在需要绘制的时候会被回调,以用来重绘客户区。
onSizeChange会在窗口大小改变的时候被调用。


    private Dnview view;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);    //沒有title notitle no title
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
 
 
        view = new Dnview(this);
 
       // setContentView(view);
        setContentView(R.layout.main);
    }
 
    private class Dnview extends View 
    {
        private Bitmap  mBitmap;
        private Paint   mPaint = new Paint();
        private Canvas  mCanvas = new Canvas();
 
 
	public Dnview(Context context)
	{
		super(context);
		// TODO Auto-generated constructor stub
 
	}
 
	@Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
                mCanvas.setBitmap(mBitmap);
 
                mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
	}
 
        @Override
        protected void onDraw(Canvas canvas)
        {
        	Log.d(TAG,"onDraw");
 
        	int h = mBitmap.getHeight();
        	int w = mBitmap.getWidth();
 
        	mCanvas.drawColor(0x7fffffff);
                mPaint.setColor(0xff00ff00);
                mCanvas.drawLine(0, 0,      w, h,      mPaint);
                mCanvas.drawLine(0, 0,      w, h/2,      mPaint);
 
                canvas.drawBitmap(mBitmap, 0,0,null);
 
        	mPaint.setColor(0xff0000ff);
        	canvas.drawLine(0, h, w, 0, mPaint);
        }
 
    }

Menu

Menu功能并不需要再实现其他接口或者继承自其它父类。它是Activity自有的功能

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
 
    	Log.d(TAG,"onCreateOptionsMenu");
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        menu.add(0, Menu.FIRST+1, 1, "open");
        menu.add(0, Menu.FIRST+2, 2, "StartService");
        menu.add(0, Menu.FIRST+3, 3, "StopService");
        menu.add(0, Menu.FIRST+4, 4, "close");
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item)	
    {
    	// TODO Auto-generated method stub
    	super.onOptionsItemSelected(item);
    	switch(item.getItemId())
    	{
	    	case Menu.FIRST + 1:
		{
			Log.d(TAG,"Menu.FIRST + 1");
			break;
		}
	    	case Menu.FIRST + 2:
		{
 
			Log.d(TAG,"Menu.FIRST + 2");
			break;
		}
	    	case Menu.FIRST + 3:
		{
			Log.d(TAG,"Menu.FIRST + 3");
			break;
		}
	    	case Menu.FIRST + 4:
		{
			Log.d(TAG,"Menu.FIRST + 4");
			break;
 
		}
	}
    	return true;
    }

启动另一个activity

启动

Android中是以Intent连接各个组件的。
用法如下:

    Intent in = new Intent(this,YdnActivity.class);
    startActivity(in);

其中YdnActivity为一个从activity继承过来的类,用法同一般的activity用法。

传递数据

并且在启动新的activity的时候还可以通过bundle机制传递给新的activity数据。
用法如下:

    //调用
    Intent in = new Intent(this,YdnActivity.class);
    double height = 172;
    String sex = "mile";
    Bundle bundle = new Bundle();
    bundle.putDouble("height",height);
    bundle.putString("sex",sex);
    in.putExtras(bundle);
    startActivity(in);
 
 
    //使用
    Bundle bundle = this.getIntent().getExtras();
    String sex = bundle.getString("sex");
    double height = bundle.getDouble("height");


返回数据