diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index c8378d498..c6e03b6f6 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -256,6 +256,15 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/ApiDemos/res/layout/fragment_arguments.xml b/samples/ApiDemos/res/layout/fragment_arguments.xml new file mode 100644 index 000000000..6541433b4 --- /dev/null +++ b/samples/ApiDemos/res/layout/fragment_arguments.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/samples/ApiDemos/res/values/attrs.xml b/samples/ApiDemos/res/values/attrs.xml index e397e4025..35f224ef5 100644 --- a/samples/ApiDemos/res/values/attrs.xml +++ b/samples/ApiDemos/res/values/attrs.xml @@ -44,4 +44,12 @@ + + + + + + + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index 122097e24..36b44c783 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -108,6 +108,12 @@ App/Fragment/Alert Dialog + App/Fragment/Arguments + Demonstrates a fragment that takes arguments + as a Bundle at runtime (on the right) or from attributes in a layout (on the left). + From Attributes + Landscape Only + App/Fragment/Hide and Show App/Fragment/Context Menu diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java new file mode 100644 index 000000000..bf1772827 --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.apis.app; + +import com.example.android.apis.R; + +import android.app.Activity; +import android.app.Fragment; +import android.app.FragmentTransaction; +import android.content.res.TypedArray; +import android.os.Bundle; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +/** + * Demonstrates a fragment that can be configured through both Bundle arguments + * and layout attributes. + */ +public class FragmentArguments extends Activity { +//BEGIN_INCLUDE(create) + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.fragment_arguments); + + if (savedInstanceState == null) { + // First-time init; create fragment to embed in activity. + FragmentTransaction ft = getFragmentManager().beginTransaction(); + Fragment newFragment = MyFragment.newInstance("From Arguments"); + ft.add(R.id.created, newFragment); + ft.commit(); + } + } +//END_INCLUDE(create) + +//BEGIN_INCLUDE(fragment) + public static class MyFragment extends Fragment { + CharSequence mLabel; + + /** + * Create a new instance of MyFragment that will be initialized + * with the given arguments. + */ + static MyFragment newInstance(CharSequence label) { + MyFragment f = new MyFragment(); + Bundle b = new Bundle(); + b.putCharSequence("label", label); + f.setArguments(b); + return f; + } + + /** + * Parse attributes during inflation from a view hierarchy into the + * arguments we handle. + */ + @Override public void onInflate(Activity activity, AttributeSet attrs, + Bundle savedInstanceState) { + super.onInflate(activity, attrs, savedInstanceState); + + TypedArray a = activity.obtainStyledAttributes(attrs, + R.styleable.FragmentArguments); + mLabel = a.getText(R.styleable.FragmentArguments_android_label); + a.recycle(); + } + + /** + * During creation, if arguments have been supplied to the fragment + * then parse those out. + */ + @Override public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Bundle args = getArguments(); + if (args != null) { + mLabel = args.getCharSequence("label", mLabel); + } + } + + /** + * Create the view for this fragment, using the arguments given to it. + */ + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.hello_world, container, false); + View tv = v.findViewById(R.id.text); + ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)"); + tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb)); + return v; + } + } +//END_INCLUDE(fragment) +}