Activity是一种可以包含用户界面的组件,主要用于和用户交互。一个App中可以有多个Activity。
import android.support.v7.ActionBarActivity; import android.os.Bundle; public void MainActivity ectends ActionBarActivity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(); setContentView(R.id.activity_main); } @Override protected void onStart(){ super.onStart(); } @Override protected void onResume(){ super.onResume(); } @Override protected void onPause(){ super.onPause(); } @Override protected void onStop(){ super.onStop(); } @Override protected void onDestroy(){ super.onDestroy(); } @Override protected void onRestart(){ super.onRestart(); } }
Activity启动后依次执行onCreate(),onStart(),onResume(),当到达onResume状态时Activity就处于可见状态,用户可以与之交互。当Activity1跳转到Activity2时,先执行Activity1的onPause(),再执行Activity2的onCreate()、onStart()、onResume(),最后执行Activity1的onStop()。
Intent:是Android程序中各组件之间进行交互的重要方式
1、显式Intent
Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); //在AndroidManifest.xml中为SecondActivity注册 <Application> <activity android:name=".SecondActivity"> </activity> </Application>
2、隐式Intent
Intent intent = new Intent("com.example.activitytest.ACTION_START"); startActivity(intent); //AndroidManifest.xml <activity name=".SecondActivity"> <intent-filter> <action android:name="com.example.activitytest.ACTION_START"> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
3、向下一个Activity传递数据
//MainActivity.java //Intent android.content.Intent.putExtra(String name, String value) String data = "Hello SecondActivity"; Intent intent = new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("extra_data",data); startActivity(intent); //SecondActivity.java Intent intent = getIntent(); String data = intent.getStringExtra("extra_data"); TextView textView = (TextView)findViewById(R.id.textView); textView.setText(data);
4、返回数据给上一个Activity
//MainActivity.java /*void android.support.v4.app.FragmentActivity.startActivityForResult (Intent intent, int requestCode)*/ package sunny.example.activityforresult; import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.view.View.OnClickListener; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button forResult = (Button)findViewById(R.id.forResult); forResult.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,SecondActivity.class); /*void android.support.v4.app.FragmentActivity.startActivityForResult(Intent intent, int requestCode)*/ startActivityForResult(intent,1);//requestCode只要是一个唯一值就可以 } }); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ switch(requestCode){ case 1: if(resultCode == RESULT_OK){ String returnData = data.getStringExtra("data_return"); TextView returnd = (TextView)findViewById(R.id.returnd); returnd.setText(returnData ); Log.i("MainActivity",returnData); } break; default: break; } } //SecondActivity.java package sunny.example.activityforresult; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SecondActivity extends ActionBarActivity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.second); Button Back = (Button)findViewById(R.id.back); Back.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.putExtra("data_return", "Hello MainActivity"); //void android.app.Activity.setResult(int resultCode, Intent data) setResult(RESULT_OK,intent); finish(); } }); } }