1.活動需要在AndroidManifest中注冊<注冊聲明要放在 application標簽內(nèi)>
2.Toast.makeText(要顯示的活動,要顯示的語句,Toast.LENGTH_SHORT/LONG).show();顯示相應(yīng)提醒方式
3.活動啟動直接調(diào)用onCreate方法
? ? 先super.onCreate(savedInstanceState);
?? 然后 setContentView(活動對應(yīng)的layout);
4.使用顯示Intent
? ? Intent intent = new Intent(上下文,目標活動);
? ? startActivity(intent);
? 使用隱式Intent
?? 設(shè)置中的和屬性
?? Intent intent = new Intent(這里填入 action的值);
?? Intent.addCategory(這里填入category的值);
?? startActivity(intent);
5.向下一個活動傳遞數(shù)據(jù)
? ? String date=“XXX”;
? ? Intent intent = new Intent(上下文,目標活動);
? ? Intent.putExtra(“定義的名字”,date);
? ? startActivity(intent);
在目標活動中
? ? Intent intent = getIntent();
? ? String date = intent.getStringExtra(“定義的名字”);
6.返回數(shù)據(jù)給上一個活動
? ? startActivityForResult(intent,請求碼)來啟動活動
? ? 在OnActivityResult(請求碼,活動返回數(shù)據(jù)處理結(jié)果,data)方法中
? ? switch(請求碼);
?? ? ? ? ? ? ? Case請求碼
?? ? ? ? ? ? ? ? ? ? ? ? if(resultCode ==活動返回數(shù)據(jù)處理結(jié)果)
?? ? ? ? ? ? ? ? ? ? ? ? String date = date.getStringExtra(“定義的名字”);//成功獲取下一個活動傳過來的數(shù)據(jù)
? ? 在目標活動中
? ? Intent intent = new Intent();
? ? Intent.putExtra(“定義的名字”,”數(shù)據(jù)”);
?? setResult(用于向上一個活動返回處理結(jié)果,intent);
?? finish;
7.活動生命周期
? ? 4種狀態(tài):運行狀態(tài)(活動處于棧頂)、暫停狀態(tài)(活動不處于棧頂 但任然可見)、停止狀態(tài)(活動不處于棧頂且完全不可見)、銷毀狀態(tài)
? ? onCreate -onStart- onResume- onPause- onStop- onDestory(onRestart)
8.活動被回收怎么辦
?? onSaveInstanceState(Bundle outState)方法臨時保存數(shù)據(jù)
?? outState.putString(“定義的名字”,”數(shù)據(jù)”);
?? 在onCreate方法中
?? 判斷 savedInstanceState 是不是為空?
?? String date = savedInstanceState.getString(“定義的名字”);
9.活動的四種啟動模式
需要在標簽中聲明啟動模式
例如:android:launchMode=“singleTop”
Standard (默認啟動模式)
singleTop
singleTask(看棧里面有沒有,如果有 直接把它之上的所有活動出棧)
singleInstance:啟用一個新的返回棧來管理活動
10.如何清楚活動需要傳遞哪些數(shù)據(jù)
?? ? 在目標活動中 添加actionStart的靜態(tài)方法
? ? actionStart(Context context,String data1,String date2)
? ? Intent intent = new Intent(context,目標活動);
? ? intent.putExtra(“定義的名字1”,data1);
? ? intent.putExtra(“定義的名字2”,data2);
? ? context.startActivity(intent);
?? 在上下文中
?? 目標活動.actionStart(上下文,”要傳送的數(shù)據(jù)1”,”要傳送的數(shù)據(jù)2”);//可以直接知道要傳哪些數(shù)據(jù)以及數(shù)據(jù)的數(shù)據(jù)類型