android基本知识(一)
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Intent intent2 = new Intent("com.example.intenttest.ACTION_START");
intent2.addCategory("com.example.intenttest.MY_CATEGORY");
startActivity(intent);
<activity
android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter >
<action android:name="com.example.intenttest.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="com.example.intenttest.MY_CATEGORY"/>
</intent-filter>
</activity>
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent,1);
Intent intent = new Intent();
intent.putExtra("extra_data", "我返回数据了");
setResult(RESULT_OK, intent);
finish();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if(RESULT_OK == resultCode){
String extra_data = data.getStringExtra("extra_data");
Log.i("extra_data",extra_data);
}
break;
default:
break;
}
};
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Log.i("Life Tag", "OnCreate");
Button normalButton = (Button)findViewById(R.id.normal_button);
Button dialogButton = (Button)findViewById(R.id.dialog_button);
EditText tempText = (EditText)findViewById(R.id.login_name);
if(null != savedInstanceState){//利用Bundle对象来还原之前在内存回收活动时,保存的关键数据
String tempData = savedInstanceState.getString("tempName");
Log.i("Tag", tempData);
tempText.setText(tempData);
}
//测试安卓活动的七个生命周期,利用对话框活动和完全覆盖活动来区别onPause和onStop方法
normalButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,NormalActivity.class);
startActivity(intent);
}
});
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,DialogActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("Life Tag", "onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("Life Tag", "OnResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("Life Tag", "OnPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("Life Tag", "onStop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("Life Tag", "onDestroy");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i("Life Tag", "OnRestart");
}
//在内存不足时回收活动,保存其主要关键数据,这在用户体验还是比较重要的了。
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
EditText tempText = (EditText)findViewById(R.id.login_name);
String name = tempText.getText().toString();
outState.putString("tempName", name);
Log.i("Name_Tag", name);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button secondButton = (Button)findViewById(R.id.secondButton);
Log.i("TAG", "The task is"+getTaskId());
secondButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_second);
Log.i("TAG", "The task is"+getTaskId());
Button thirdButton = (Button)findViewById(R.id.third_button);
thirdButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
}
});
}
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_third);
Log.i("TAG", "The task is"+getTaskId());
Button firstButton = (Button)findViewById(R.id.firstButton);
firstButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ThirdActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。