同一个应用程序中的Activity切换通常一个应用程序中需要多个UI 屏幕,也就需要多个Activity 类,并且在这些 Activity 之间进行切换,这种切换就是通过 Intent 机制来实现的。
例如
Intent intent = new Intent(getActivity(), DetailActivity.class);// intent.putExtra(Intent.EXTRA_TEXT, forecast);//添加附带信息 startActivity(intent);//开启Activity
例如
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,根视图
---------------------------------------------------------------------------------------------------------------------
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>