zoukankan      html  css  js  c++  java
  • 简单程序用于熟悉Activity生命周期

    FirstActivity.java

    public class FirstActivity extends Activity {
        /** Called when the activity is first created. */
        private Button myButton;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            System.out.println("FirstAcvity ---> onCreate");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            myButton = (Button)findViewById(R.id.myButton);
            myButton.setText("启动第二个Activity");
            myButton.setOnClickListener(new ButtonOnClickListener());
        }
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            System.out.println("FirstAcvity --->onDestory");
            super.onDestroy();
        }
    
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            System.out.println("FirstAcvity --->onPause");
            super.onPause();
        }
    
        @Override
        protected void onRestart() {
            // TODO Auto-generated method stub
            System.out.println("FirstAcvity --->onRestart");
            super.onRestart();
        }
    
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            System.out.println("FirstAcvity --->onResume");
            super.onResume();
        }
    
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            System.out.println("FirstAcvity --->onStart");
            super.onStart();
        }
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            System.out.println("FirstAcvity --->onStop");
            super.onStop();
        }
        
        class ButtonOnClickListener implements OnClickListener{
    
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(FirstActivity.this,SecondActivity.class);
                FirstActivity.this.startActivity(intent);
            }
            
        }
        
    }

    SecondActivity.java

    public class SecondActivity extends Activity {
    
    	private Button secondButton;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		
    		// TODO Auto-generated method stub
    		System.out.println("SecondActivity--->onCreate");
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.second);
    		secondButton = (Button)findViewById(R.id.secondButton);
    		Gallery g = (Gallery)findViewById(R.id.secondButton);
    		secondButton.setOnClickListener(new ButtonOnClickListener());
    		
    	}
    
    	@Override
    	protected void onDestroy() {
    		// TODO Auto-generated method stub
    		System.out.println("SecondActivity--->onDestory");
    		super.onDestroy();
    	}
    
    	@Override
    	protected void onPause() {
    		// TODO Auto-generated method stub
    		System.out.println("SecondActivity--->onPause");
    		super.onPause();
    	}
    
    	@Override
    	protected void onRestart() {
    		// TODO Auto-generated method stub
    		System.out.println("SecondActivity--->onRestart");
    		super.onRestart();
    	}
    
    	@Override
    	protected void onResume() {
    		// TODO Auto-generated method stub
    		System.out.println("SecondActivity--->onResume");
    		super.onResume();
    	}
    
    	@Override
    	protected void onStart() {
    		// TODO Auto-generated method stub
    		System.out.println("SecondActivity--->onStart");
    		super.onStart();
    	}
    
    	@Override
    	protected void onStop() {
    		// TODO Auto-generated method stub
    		System.out.println("SecondActivity--->onStop");
    		super.onStop();
    	}
    	class ButtonOnClickListener implements OnClickListener{
    
    		@Override
    		public void onClick(View v) {
    			Intent intent = new Intent();
    			intent.setClass(SecondActivity.this,FirstActivity.class);
    			SecondActivity.this.startActivity(intent);
    		}
    		
    	}
    }
    

     启动第一个Activity,然后启动第二个Activity,然后按返回键,然后再启动第二个Activity...查看输出结果。

  • 相关阅读:
    简直不敢相信...
    halcon 保存Region [原创]
    VS2015 下载链接
    C#【数据转换】十进制yte[]相互转换
    C# 复制窗体问题完美解决办法
    TextBox 保持固定长度,添加新行滚动到最后,跨线程。
    mysql isnull
    C#跨线程访问控件[我的记录]
    C# 常用类-IO-ClassExcelExport
    C# 常用类-IO-ClassXML
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3138114.html
Copyright © 2011-2022 走看看