/ABSherlock-Fragments/src/com/actionbarsherlock/sample/fragments/FragmentArgumentsSupport.java
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
17package com.actionbarsherlock.sample.fragments;
18
19import android.app.Activity;
20import android.content.res.TypedArray;
21import android.os.Bundle;
22import android.support.v4.app.Fragment;
23import android.support.v4.app.FragmentTransaction;
24import android.util.AttributeSet;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.TextView;
29import com.actionbarsherlock.app.SherlockFragment;
30import com.actionbarsherlock.app.SherlockFragmentActivity;
31
32/**
33 * Demonstrates a fragment that can be configured through both Bundle arguments
34 * and layout attributes.
35 */
36public class FragmentArgumentsSupport extends SherlockFragmentActivity {
37
38 @Override protected void onCreate(Bundle savedInstanceState) {
39 setTheme(SampleList.THEME); //Used for theme switching in samples
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.fragment_arguments_support);
42
43 if (savedInstanceState == null) {
44 // First-time init; create fragment to embed in activity.
45 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
46 Fragment newFragment = MyFragment.newInstance("From Arguments");
47 ft.add(R.id.created, newFragment);
48 ft.commit();
49 }
50 }
51
52
53
54 public static class MyFragment extends SherlockFragment {
55 CharSequence mLabel;
56
57 /**
58 * Create a new instance of MyFragment that will be initialized
59 * with the given arguments.
60 */
61 static MyFragment newInstance(CharSequence label) {
62 MyFragment f = new MyFragment();
63 Bundle b = new Bundle();
64 b.putCharSequence("label", label);
65 f.setArguments(b);
66 return f;
67 }
68
69 /**
70 * Parse attributes during inflation from a view hierarchy into the
71 * arguments we handle.
72 */
73 @Override public void onInflate(Activity activity, AttributeSet attrs,
74 Bundle savedInstanceState) {
75 super.onInflate(activity, attrs, savedInstanceState);
76
77 TypedArray a = activity.obtainStyledAttributes(attrs,
78 R.styleable.FragmentArguments);
79 mLabel = a.getText(R.styleable.FragmentArguments_android_label);
80 a.recycle();
81 }
82
83 /**
84 * During creation, if arguments have been supplied to the fragment
85 * then parse those out.
86 */
87 @Override public void onCreate(Bundle savedInstanceState) {
88 super.onCreate(savedInstanceState);
89
90 Bundle args = getArguments();
91 if (args != null) {
92 CharSequence label = args.getCharSequence("label");
93 if (label != null) {
94 mLabel = label;
95 }
96 }
97 }
98
99 /**
100 * Create the view for this fragment, using the arguments given to it.
101 */
102 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
103 Bundle savedInstanceState) {
104 View v = inflater.inflate(R.layout.hello_world, container, false);
105 View tv = v.findViewById(R.id.text);
106 ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
107 tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
108 return v;
109 }
110 }
111
112}