同一個應用程式中的Activity切換通常一個應用程式中需要多個UI 螢幕,也就需要多個Activity 類,并且在這些 Activity 之間進行切換,這種切換就是通過 Intent 機制來實作的。
例如
Intent intent = new Intent(getActivity(), DetailActivity.class);//
intent.putExtra(Intent.EXTRA_TEXT, forecast);//添加附帶資訊
startActivity(intent);//開啟Activity
在DetailActivity.class裡面接受intent傳來的附加資訊
例如
Intent intent = getActivity().getIntent();
if(intent != null && intent.hasExtra(Intent.EXTRA_TEXT)){
String forecast = intent.getStringExtra(Intent.EXTRA_TEXT);
((TextView)rootView.findViewById(R.id.forecastDetail)).setText(forecast);//something to do
--------------------------------------------------------------------------------------------------------------------------
開啟其它應用的intent,系統如何知道其它應用可以處理此intent
比如地圖應用
在它的manifest.xml裡面 activity 有個 intent篩選器
<intent-filter >
<action:name ="android.intent.action.VIEW"/>
<data android:scheme="geo"/>
</intent-filter>
---------------------------------------------------------------------------------------------------------------------
讀取預設配置資訊
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
position = prefs.getString(getString(R.string.pref_location_key),getString(R.string.pref_location_default));
weatherTask.execute(position);
----------------------------------------------------------------------------------------------------------------------
對于PreferenceActivity中addPreferencesFromResource(R.xml.pref_general)函數的棄用
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
然後将其添加到Activity
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
}
在安卓布局檔案中添加控件<Fragment />,系統定義的此Fragment的id為android.R.id.content,根視圖
---------------------------------------------------------------------------------------------------------------------
Basic Activity 建立項目時候帶fragment.xml 有個選項勾選的,use a fragment 。luancher activity 作為點選應用進去的主界面。 ------------------------------------------------------------------------------------------------------------------- 分享菜單 fragment可以通過實作 onCreateOptionMenu() 提供菜單項給activity的選項菜單。為了使這個方法接收調用,無論如何, 你必須在 onCreate() 期間調用 setHasOptionsMenu() 來指出fragment願意添加item到選項菜單(否則, fragment将接收不到對 onCreateOptionsMenu()的調用).
随後從fragment添加到Option菜單的任何項,都會被追加到現有菜單項的後面.當一個菜單項被選擇, fragment也會接收到對onOptionsItemSelected() 的回調.就是說fragment中的菜單項包含了活動中定義的菜單。
class DetailFragment
public DetailFragment() {
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detailfragment,menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null){
mShareActionProvider.setShareIntent(createShareForecastIntent());
}else{
Log.e(LOG_TAG,"share action provider is null!");
}
}
//FLAG_ACTIVITY_NEW_DOCUMENT instead of FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
private Intent createShareForecastIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,mForecastStr+FORECAST_SHARE_TAG);
return shareIntent;
}
菜單配置檔案
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.sunshine.app.DetailActivity">
<item
android:id="@+id/action_share"
android:title="@string/action_share"
app:showAsAction="always"
app:actionProviderClass=
"android.support.v7.widget.ShareActionProvider" />
</menu>