下面列出了android.app.Fragment#setArguments ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new AnimalFragment();
Bundle args = new Bundle();
args.putInt(AnimalFragment.ARG_ANIMAL_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mAnimalTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void onItemSelected(long id) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putLong(ContactDetailFragment.CONTACT_ID, id);
Fragment fragment = new ContactDetailFragment();
fragment.setArguments(arguments);
getFragmentManager().beginTransaction()
.replace(R.id.contact_detail_container, fragment)
.commitAllowingStateLoss();
} else {
Intent detailIntent = new Intent(this, ContactDetailActivity.class);
detailIntent.putExtra(ContactDetailFragment.CONTACT_ID, id);
startActivity(detailIntent);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new SampleFragment();
Bundle args = new Bundle();
args.putInt(SampleFragment.ARG_IMAGE_RES, mCityImages[position]);
args.putInt(SampleFragment.ARG_ACTION_BG_RES, R.drawable.fading_actionbar_ab_background);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mCityNames[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case Constants.REQUEST_CODE_LOGIN:
inputManager.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
navigationView.getMenu().performIdentifierAction(R.id.nav_wallet, 0);
}
if (data != null) {
if (Intent.ACTION_VIEW.equals(data.getAction())) {
QRCode qrCode = new QRCode();
Uri uri = data.getData();
qrCode.setAddress(uri.getQueryParameter("address:"));
qrCode.setAddress(uri.getQueryParameter("amount:"));
qrCode.setAddress(uri.getQueryParameter("message:"));
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.QRCODE, qrCode);
Fragment fragment = new NewTransferFragment();
fragment.setArguments(bundle);
showFragment(fragment, true);
}
}
}
/** Swaps fragments in the main content view */
private void selectItem(int position) {
// Create a new fragment and specify the planet to show based on position
Fragment fragment = new OpertingSystemFragment();
Bundle args = new Bundle();
args.putInt(OpertingSystemFragment.ARG_OS, position);
fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
// TODO Replace current fragment in R.id.content_frame with new fragment
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
getActionBar().setTitle((mPlanetTitles[position]));
mDrawerLayout.closeDrawer(mDrawerList);
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new SampleFragment();
Bundle args = new Bundle();
args.putInt(SampleFragment.ARG_IMAGE_RES, mCityImages[position]);
args.putInt(SampleFragment.ARG_ACTION_BG_RES, R.drawable.ab_background);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mCityNames[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new SampleFragment();
Bundle args = new Bundle();
args.putInt(SampleFragment.ARG_IMAGE_RES, mCityImages[position]);
args.putInt(SampleFragment.ARG_ACTION_BG_RES, R.drawable.fading_actionbar_ab_background);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mCityNames[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
public void prepare(Fragment toFragment) {
final Bundle transitionBundle = TransitionBundleFactory.createTransitionBundle(context, fromView, bitmap);
if (Build.VERSION.SDK_INT >= 21) {
toFragment.setSharedElementEnterTransition(new ChangeBounds());
toFragment.setSharedElementReturnTransition(new ChangeBounds());
}
toFragment.setArguments(transitionBundle);
}
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment).commit();
return true;
}
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_single_fragment);
Bundle b = getIntent().getExtras();
Class<?> fragmentClass = (Class<?>) b.get(FRAGMENT_PARAM);
if (bundle == null) {
Fragment f = Fragment.instantiate(this, fragmentClass.getName());
f.setArguments(b);
getFragmentManager().beginTransaction().replace(R.id.fragment, f, fragmentClass.getName()).commit();
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
private void createFragments() {
// Begin a new FragmentTransaction
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// This fragment shows ProfileListFragment as a sidebar (only seen in tablet mode landscape)
if(!(getFragmentManager().findFragmentById(R.id.profileList) instanceof ProfileListFragment))
transaction.replace(R.id.profileList, new ProfileListFragment(), "ProfileListFragment");
// This fragment shows ProfileListFragment in the main screen area (only seen on phones and tablet mode portrait),
// but only if it doesn't already contain ProfileViewFragment or ProfileEditFragment.
// If ProfileListFragment is already showing in the sidebar, use WelcomeFragment instead
if(!((getFragmentManager().findFragmentById(R.id.profileViewEdit) instanceof ProfileEditFragment)
|| (getFragmentManager().findFragmentById(R.id.profileViewEdit) instanceof ProfileViewFragment))) {
SharedPreferences prefMain = U.getPrefMain(this);
if(prefMain.getBoolean("show-welcome-message", false)
|| (getFragmentManager().findFragmentById(R.id.profileViewEdit) == null
&& findViewById(R.id.layoutMain).getTag().equals("main-layout-large"))
|| ((getFragmentManager().findFragmentById(R.id.profileViewEdit) instanceof ProfileListFragment)
&& findViewById(R.id.layoutMain).getTag().equals("main-layout-large"))) {
// Show welcome message
Bundle bundle = new Bundle();
bundle.putBoolean("show-welcome-message", prefMain.getBoolean("show-welcome-message", false));
Fragment fragment = new WelcomeFragment();
fragment.setArguments(bundle);
transaction.replace(R.id.profileViewEdit, fragment, "NoteListFragment");
} else if(findViewById(R.id.layoutMain).getTag().equals("main-layout-normal"))
transaction.replace(R.id.profileViewEdit, new ProfileListFragment(), "NoteListFragment");
}
// Commit fragment transaction
transaction.commit();
}
public static
Fragment newInstance(int position)
{
Fragment fragment = new ListFragmentTag();
Bundle bundle = new Bundle();
bundle.putInt(POSITION_KEY, position);
fragment.setArguments(bundle);
return fragment;
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new ImageHolderFragment();
Bundle args = new Bundle();
args.putInt(ImageHolderFragment.RES_ID, mImageIds[i]);
args.putString(ImageHolderFragment.POS, String.valueOf(i));
fragment.setArguments(args);
return fragment;
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, show the tab contents in the
// container view.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
tab.getPosition() + 1);
fragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment).commit();
}
/**
* Switches to the passed fragment
* If the currently shown fragment has the same type as the argument, does nothing. Otherwise,
* removes the current fragment and makes a new fragment of the passed in type and shows it
* @param type the fragment to switch to
* @param arguments arguments to give to the fragment
*/
private void swapScreens(FragmentType type, @Nullable Bundle arguments) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
// if we already have a fragment and that fragment is not the fragment we want to show, remove it
// otherwise, it is the fragment we want to show, so we're done, just return and abort the transaction
if (manager.findFragmentByTag("fragment") != null) {
if (((HiddenSettingsFragment) manager.findFragmentByTag("fragment")).getType() != type) {
transaction.remove(manager.findFragmentByTag("fragment"));
} else {
return;
}
}
// Make a new fragment
Fragment newFragment = null;
switch (type) {
case SETTINGS_LIST:
newFragment = new SettingsListFragment();
break;
case JANITOR_LOGIN:
newFragment = new JanitorLoginFragment();
break;
case THREAD_WATCHER:
newFragment = new ThreadWatcherFragment();
break;
case AWOO_ENDPOINT:
newFragment = new AwooEndpointFragment();
break;
case DEBUG_SETTINGS_LIST:
newFragment = new DebugSettingsListFragment();
break;
case COLOR_LIST:
newFragment = new ColorListFragment();
break;
case PROPERTY_EDITOR:
newFragment = new PropertyEditorFragment();
break;
case PROPERTY_EDITOR_NEW:
newFragment = new NewPropertyFragment();
break;
case PROPERTIES_LIST:
newFragment = new PropertiesListFragment();
break;
case HIDDEN_LIST:
newFragment = new HiddenThreadListFragment();
break;
case NOTIFICATION_SETTINGS:
newFragment = new NotificationSettingsFragment();
break;
case COLOR_PICKER:
newFragment = new ColorPickerFragment();
break;
}
newFragment.setArguments(arguments);
// Put the fragment in our layout
//transaction.add(newFragment, "fragment");
transaction.replace(R.id.userscript_activity_main_fragment, newFragment, "fragment");
transaction.addToBackStack("fragment"); // TODO is this needed?
transaction.commit();
}
@Override
public void onClick(DialogInterface dialogInterface, int which) {
ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
Fragment fragment;
final Bundle bundle = new Bundle();
switch (which) {
case 0:
ClipData clipAddress = ClipData.newPlainText(getActivity().getString(R.string.address), address);
clipboard.setPrimaryClip(clipAddress);
break;
case 1:
fragment = new TangleExplorerTabFragment();
bundle.putString(Constants.TANGLE_EXPLORER_SEARCH_ITEM, address);
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.showFragment(fragment);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
EventBus.getDefault().post(bundle);
}
}, 300);
break;
case 2:
if (!isAddressUsed) {
QRCode qrCode = new QRCode();
qrCode.setAddress(address);
bundle.putParcelable(Constants.QRCODE, qrCode);
fragment = new GenerateQRCodeFragment();
fragment.setArguments(bundle);
getActivity().getFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.replace(R.id.container, fragment, null)
.addToBackStack(null)
.commit();
} else {
Snackbar.make(getActivity().findViewById(R.id.drawer_layout), getString(R.string.messages_address_used), Snackbar.LENGTH_LONG).show();
}
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// To check if the user has enabled GPS
locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// We now add the Drawer to the View, and populate it with the resources
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerView = (NavigationView) findViewById(R.id.nav_view);
//setting up selected item listener
mDrawerView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// menuItem.setChecked(true);
selectItem(menuItem.getTitle());
mDrawerLayout.closeDrawers();
return true;
}
});
mDrawerTitle = mTitle = getTitle();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
/* Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
// With this, we add or remove Toolbar buttons depending on drawer state
}
/* Called when a drawer has settled in a completely open state.*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
Fragment firstFragment = null;
// k3b: when startet via geo-uri then start map with these data else start home
// where you can enter parameters
Uri uri = getIntent().getData();
String geoUriString = (uri != null) ? uri.toString() : null;
GeoUri parser = new GeoUri(GeoUri.OPT_DEFAULT);
IGeoPointInfo geoPoint = parser.fromUri(geoUriString);
if ((geoPoint != null) && ((geoPoint.getName() != null) || !GeoPointDto.isEmpty(geoPoint))) {
Resources res = getResources();
Bundle args = new Bundle();
args.putInt(HomeFragment.EXTRA_OPTIONS[3], HomeFragment.METHOD_URI);
args.putString(HomeFragment.EXTRA_OPTIONS[4], geoUriString);
args.putInt(HomeFragment.EXTRA_OPTIONS[0], res.getInteger(R.integer.default_maxPOI));
args.putDouble(HomeFragment.EXTRA_OPTIONS[1], res.getInteger(R.integer.default_range));
firstFragment = new MapFragment();
firstFragment.setArguments(args);
} else {
// Create a new Fragment to be placed in the activity layout
firstFragment = new HomeFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
}
// Add the fragment to the 'fragment_container' FrameLayout
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if(key.equals(PREF_KEY_EXCHANGE)) {
ListPreference listPref = (ListPreference) preference;
mExchangeIndex = listPref.findIndexOfValue((String) newValue);
String exchangeName = (String) listPref.getEntries()[mExchangeIndex];
listPref.setSummary(exchangeName);
mPairIndex = 0;
updatePairsList((String) newValue, exchangeName, null);
} else if(key.equals(PREF_KEY_PAIR)) {
mPairIndex = Integer.parseInt((String) newValue);
preference.setSummary(mPairs.get(mPairIndex).toString());
updateDependentOnPairAux();
} else if(key.equals(PREF_KEY_TYPE)) {
Fragment creationFrag;
Bundle args = new Bundle();
args.putInt("exchangeIndex", mExchangeIndex);
args.putInt("pairIndex", mPairIndex);
args.putString("alertType", mAlarmAlertTypePref.getValue());
args.putString("alertSound", mAlertSoundPref.getValue());
args.putString("vibrate", mVibratePref.getValue());
if(newValue.equals(PREF_VALUE_PRICE_CHANGE)) {
creationFrag = new PriceChangeAlarmCreationFragment();
} else { // newValue.equals(PREF_VALUE_PRICE_HIT))
creationFrag = new PriceHitAlarmCreationFragment();
}
creationFrag.setArguments(args);
mEnclosingActivity.getFragmentManager().beginTransaction().replace(android.R.id.content, creationFrag).commit();
} else if(key.equals(PREF_KEY_UPDATE_INTERVAL)) {
preference.setSummary(mEnclosingActivity.getString(R.string.seconds_abbreviation, newValue));
} else if(key.equals(PREF_KEY_ALARM_ALERT_TYPE)) {
ListPreference alertTypePref = (ListPreference) preference;
String alertType = (String) newValue;
alertTypePref.setSummary(alertTypePref.getEntries()[alertTypePref.findIndexOfValue(alertType)]);
// Change selectable ringtones according to the alert type
mAlertSoundPref.setRingtoneType(alertType);
mAlertSoundPref.setDefaultValue();
} else if(key.equals(PREF_KEY_ALARM_ALERT_SOUND)) {
// Nothing to do.
} else if(key.equals(PREF_KEY_ALARM_VIBRATE)) {
ListPreference vibratePref = (ListPreference) preference;
vibratePref.setSummary(vibratePref.getEntries()[vibratePref.findIndexOfValue((String) newValue)]);
} else if(key.equals(PREF_KEY_SNOOZE_ON_RETRACE)) {
// Nothing to do.
} else {
Log.d("No behavior for " + key);
}
return true;
}
private void selectItem(int position) {
Bundle args = new Bundle();
args.putString(CenterFragment.DISK_PATH,
diskAdapter.getList().get(position).getPath());
Fragment fragment = new CenterFragment();
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
if (!application.isTablet()) {
mDrawerLayout.closeDrawer(left_drawer_LinearLayout);
}
}