首页 > 代码库 > 01_Activity生命周期及传递数据
01_Activity生命周期及传递数据
1. Activity的生命周期:
2. Activity启动另一个Activity,并传递数据:
package com.example.activitydemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity { protected static final String EXTRA_MESSAGE = "com.example.activitydemo.MESSAGE"; public Button btnActivityChange; public EditText editMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnActivityChange = (Button) findViewById(R.id.btn1); editMessage = (EditText) findViewById(R.id.editMessage); btnActivityChange.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, Activity2.class); String message = editMessage.getText().toString(); i.putExtra(EXTRA_MESSAGE, message); startActivity(i); } }); }}
package com.example.activitydemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class Activity2 extends Activity { private Button btnBack; private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity2); btnBack = (Button) findViewById(R.id.btnBack); textView1 = (TextView) findViewById(R.id.textView1); Intent intent = getIntent(); textView1.setText(intent.getStringExtra(MainActivity.EXTRA_MESSAGE)); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); }}
01_Activity生命周期及传递数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。