/ABSherlock-Fragments/src/com/actionbarsherlock/sample/fragments/FragmentArgumentsSupport.java

https://bitbucket.org/ayastrebov/android-actionbarsherlock · Java · 112 lines · 62 code · 15 blank · 35 comment · 7 complexity · 4df2ca5fc945ee7797b2d4e0eb381089 MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.actionbarsherlock.sample.fragments;
  17. import android.app.Activity;
  18. import android.content.res.TypedArray;
  19. import android.os.Bundle;
  20. import android.support.v4.app.Fragment;
  21. import android.support.v4.app.FragmentTransaction;
  22. import android.util.AttributeSet;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.widget.TextView;
  27. import com.actionbarsherlock.app.SherlockFragment;
  28. import com.actionbarsherlock.app.SherlockFragmentActivity;
  29. /**
  30. * Demonstrates a fragment that can be configured through both Bundle arguments
  31. * and layout attributes.
  32. */
  33. public class FragmentArgumentsSupport extends SherlockFragmentActivity {
  34. @Override protected void onCreate(Bundle savedInstanceState) {
  35. setTheme(SampleList.THEME); //Used for theme switching in samples
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.fragment_arguments_support);
  38. if (savedInstanceState == null) {
  39. // First-time init; create fragment to embed in activity.
  40. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  41. Fragment newFragment = MyFragment.newInstance("From Arguments");
  42. ft.add(R.id.created, newFragment);
  43. ft.commit();
  44. }
  45. }
  46. public static class MyFragment extends SherlockFragment {
  47. CharSequence mLabel;
  48. /**
  49. * Create a new instance of MyFragment that will be initialized
  50. * with the given arguments.
  51. */
  52. static MyFragment newInstance(CharSequence label) {
  53. MyFragment f = new MyFragment();
  54. Bundle b = new Bundle();
  55. b.putCharSequence("label", label);
  56. f.setArguments(b);
  57. return f;
  58. }
  59. /**
  60. * Parse attributes during inflation from a view hierarchy into the
  61. * arguments we handle.
  62. */
  63. @Override public void onInflate(Activity activity, AttributeSet attrs,
  64. Bundle savedInstanceState) {
  65. super.onInflate(activity, attrs, savedInstanceState);
  66. TypedArray a = activity.obtainStyledAttributes(attrs,
  67. R.styleable.FragmentArguments);
  68. mLabel = a.getText(R.styleable.FragmentArguments_android_label);
  69. a.recycle();
  70. }
  71. /**
  72. * During creation, if arguments have been supplied to the fragment
  73. * then parse those out.
  74. */
  75. @Override public void onCreate(Bundle savedInstanceState) {
  76. super.onCreate(savedInstanceState);
  77. Bundle args = getArguments();
  78. if (args != null) {
  79. CharSequence label = args.getCharSequence("label");
  80. if (label != null) {
  81. mLabel = label;
  82. }
  83. }
  84. }
  85. /**
  86. * Create the view for this fragment, using the arguments given to it.
  87. */
  88. @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
  89. Bundle savedInstanceState) {
  90. View v = inflater.inflate(R.layout.hello_world, container, false);
  91. View tv = v.findViewById(R.id.text);
  92. ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
  93. tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
  94. return v;
  95. }
  96. }
  97. }