2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.apis.support.app;
19 import com.example.android.apis.R;
20 import com.example.android.apis.app.SendResult;
22 import android.support.v4.app.Fragment;
23 import android.support.v4.app.FragmentActivity;
24 import android.support.v4.app.FragmentTransaction;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.text.Editable;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.view.ViewGroup;
33 import android.widget.Button;
34 import android.widget.FrameLayout;
35 import android.widget.TextView;
37 public class FragmentReceiveResultSupport extends FragmentActivity {
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
43 ViewGroup.LayoutParams.MATCH_PARENT,
44 ViewGroup.LayoutParams.MATCH_PARENT);
45 FrameLayout frame = new FrameLayout(this);
46 frame.setId(R.id.simple_fragment);
47 setContentView(frame, lp);
49 if (savedInstanceState == null) {
50 // Do first time initialization -- add fragment.
51 Fragment newFragment = new ReceiveResultFragment();
52 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
53 ft.add(R.id.simple_fragment, newFragment).commit();
57 public static class ReceiveResultFragment extends Fragment {
58 // Definition of the one requestCode we use for receiving resuls.
59 static final private int GET_CODE = 0;
61 private TextView mResults;
63 private OnClickListener mGetListener = new OnClickListener() {
64 public void onClick(View v) {
65 // Start the activity whose result we want to retrieve. The
66 // result will come back with request code GET_CODE.
67 Intent intent = new Intent(getActivity(), SendResult.class);
68 startActivityForResult(intent, GET_CODE);
73 public void onCreate(Bundle savedInstanceState) {
74 super.onCreate(savedInstanceState);
78 public void onSaveInstanceState(Bundle outState) {
79 super.onSaveInstanceState(outState);
83 public View onCreateView(LayoutInflater inflater, ViewGroup container,
84 Bundle savedInstanceState) {
85 View v = inflater.inflate(R.layout.receive_result, container, false);
87 // Retrieve the TextView widget that will display results.
88 mResults = (TextView)v.findViewById(R.id.results);
90 // This allows us to later extend the text buffer.
91 mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);
93 // Watch for button clicks.
94 Button getButton = (Button)v.findViewById(R.id.get);
95 getButton.setOnClickListener(mGetListener);
101 * This method is called when the sending activity has finished, with the
102 * result it supplied.
105 public void onActivityResult(int requestCode, int resultCode, Intent data) {
106 // You can use the requestCode to select between multiple child
107 // activities you may have started. Here there is only one thing
109 if (requestCode == GET_CODE) {
111 // We will be adding to our text.
112 Editable text = (Editable)mResults.getText();
114 // This is a standard resultCode that is sent back if the
115 // activity doesn't supply an explicit result. It will also
116 // be returned if the activity failed to launch.
117 if (resultCode == RESULT_CANCELED) {
118 text.append("(cancelled)");
120 // Our protocol with the sending activity is that it will send
121 // text in 'data' as its result.
123 text.append("(okay ");
124 text.append(Integer.toString(resultCode));
127 text.append(data.getAction());