同一個應用程序中的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);
}
}
然後將其添加到Activitypublic 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,根視圖---------------------------------------------------------------------------------------------------------------------
隨後從fragment添加到Option菜單的任何項,都會被追加到現有菜單項的後面.當一個菜單項被選擇, fragment也會接收到對onOptionsItemSelected() 的回調.就是說fragment中的菜單項包含了活動中定義的菜單。
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>