Activity学习认知

1.activity生命周期


The entire lifecycle of an activity is defined by the following Activity methods. All of these are hooks that you can override to do appropriate work when the activity changes state. All activities will implement onCreate(Bundle) to do their initial setup; many will also implement onPause() to commit changes to data and otherwise prepare to stop interacting with the user. You should always call up to your superclass when implementing these methods.

翻译:activity的全部生命周期通过以下几个方法定义。当activity的状态变化时你可以根据功能来重写相应的方法。所有的activity必须实现onCreate(Bundle) 来初始化;当打算提交更改的数据或者停止与用户的互动是可能需要重写onPause() 方法。当重写这些方法时你必须调用父类的方法。

2.activity间的跳转和值传递

    显式跳转

public class MainActivity extends Activity {

	private EditText nameEt;
	private Button ceshiBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		nameEt = (EditText)findViewById(R.id.nameEt);
		ceshiBtn = (Button)findViewById(R.id.ceshiBtn);
		Intent intent = new Intent(this,RpCaculatorActivity.class);
		startActivity(intent);
                //startActivityForResult(intent, 1000);
	}
}

   隐跳转

public class MainActivity extends Activity {

	private EditText nameEt;
	private Button ceshiBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		nameEt = (EditText)findViewById(R.id.nameEt);
		ceshiBtn = (Button)findViewById(R.id.ceshiBtn);
		Intent intent = new Intent("com.example.rpcaculator.test2");
                startActivity(intent);
                //startActivityForResult(intent, 1000);
	}
}

如果通过startActivityForResult这个方法进行activity跳转的可以获取传回值。

值传递:1、直接通过intent的自带的方法进行值传递,其传值可以使字符串、对象、list等具体可以查看其方法。

                       如:Intent intent = new Intent(this,RPCacultorActivity.class);

                               intent.intent.putExtra("name", "1111");

                               在下一个activity中可以通过以下方式获得

                                Intent intent = this.getIntent();
                String name = intent.getStringExtra("name");

                 2、通过Bundle进行数据传递

                       如:Intent intent = new Intent(this,RPCacultorActivity.class);

                               Bundle data = new Bundle();

                               data.putString("name","liu");

                               intent.putExtras(data);

                               在下一个activity中通过以下方式获得相应值:

                               Bundle data = this.getIntent().getExtras();
                String name = data.getString("name");



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值