首先要學會怎麼使用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主題便可變成彈出的對話框.很神奇吧,....就到這,必定要測試下週期的感受,才能真的理解,,,,,