首先要学会怎么使用intent进行activity之间的转换和传递数据,很简单转换就是startActivity();方法,但是intent中可以使用setExtra来添加要传递的数据.(特殊情况是从第二个Activity中返回数据到第一个Activity.主要是使用startActivityforResult(inten,特征值)来启动第二个Activity就可以在第二个关闭时返回数据到第一个);我去做一个测试,一会贴代码出来.
这是第一个页面打开第二界面的代码逻辑
Button button = (Button) findViewById(R.id.btn_1);
textView = (TextView) findViewById(R.id.text1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivityForResult(intent, 1); } });这是第二个代码返回数据逻辑
public class SecondActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("data", "boom boom boom"); setResult(1, intent); finish();} }); }}
然后需要在第一个页面重写OnActivityResult函数即可,三个参数分别是你传出的requestcode和结果返回的resultcode还有结果返回的intent和他携带的extra中的数据,我这是一个String ,让他们显示在了TextView中!***(如果你按返回键也会回到第一个界面,那么我们需要重写onBackPressed函数)
下面进入正文,Activity的生命周期:分别为onCreat,onStart,onResume,onPause, onStop,onDestory,onRestart!!!这五个状态.具体不多说,你要知道这五个状态怎么互相转换,和分别是什么状态才行.(只有OnSTop也就是完全不可见了的时候再回来才会用onrestart记住!!!)
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button button1,button2;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("lkk", "oncreat"); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); }@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; }
@Override
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }@Override
public void onClick(View v) { switch (v.getId()) { case R.id.button1 : Intent intent = new Intent(MainActivity.this,NarmolActivity.class); startActivity(intent); break;case R.id.button2 :
Intent intent2 = new Intent(MainActivity.this,DialogActivity.class); startActivity(intent2); break; default: break; } }@Overrideprotected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.d("lkk", "onstart");}@Overrideprotected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.d("lkk", "onresume");}@Override
protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.d("lkk", "onPause");}@Override
protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.d("lkk", "ondestory");}@Overrideprotected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.d("lkk", "onstop");}@Overrideprotected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.d("lkk", "onRestart");}}以上代码是测试周期代码,没有贴出其他两个页面的代码,就是两个空页面.但是dialog那个只要在manufast文件中添加一个theme.dialog主题即可变成弹出的对话框.很神奇吧,....就到这,一定要测试下周期的感觉,才能真的理解,,,,,