首先要学会怎么使用intent进行activity之间的转换和传递数据,很简单转换就是startActivity();方法,可是intent中能够使用setExtra来添加要传递的数据.(特殊状况是从第二个Activity中返回数据到第一个Activity.主要是使用startActivityforResult(inten,特征值)来启动第二个Activity就能够在第二个关闭时返回数据到第一个);我去作一个测试,一会贴代码出来.ide
这是第一个页面打开第二界面的代码逻辑函数
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);
}
});测试
这是第二个代码返回数据逻辑this
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();}
});
}rest
}code
而后须要在第一个页面重写OnActivityResult函数便可,三个参数分别是你传出的requestcode和结果返回的resultcode还有结果返回的intent和他携带的extra中的数据,我这是一个String ,让他们显示在了TextView中!***(若是你按返回键也会回到第一个界面,那么咱们须要重写onBackPressed函数)xml
下面进入正文,Activity的生命周期:分别为onCreat,onStart,onResume,onPause, onStop,onDestory,onRestart!!!这五个状态.具体很少说,你要知道这五个状态怎么互相转换,和分别是什么状态才行.(只有OnSTop也就是彻底不可见了的时候再回来才会用onrestart记住!!!)生命周期
public class MainActivity extends ActionBarActivity implements OnClickListener{ci
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);
}get
@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;
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("lkk", "onstart");
}
@Override
protected 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");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("lkk", "onstop");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d("lkk", "onRestart");
}
}
以上代码是测试周期代码,没有贴出其余两个页面的代码,就是两个空页面.可是dialog那个只要在manufast文件中添加一个theme.dialog主题便可变成弹出的对话框.很神奇吧,....就到这,必定要测试下周期的感受,才能真的理解,,,,,