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 17 package com.example.android.apis.app; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.app.Fragment; 23 import android.app.FragmentTransaction; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.text.Editable; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.View.OnClickListener; 30 import android.view.ViewGroup; 31 import android.widget.Button; 32 import android.widget.FrameLayout; 33 import android.widget.TextView; 34 35 public class FragmentReceiveResult extends Activity { 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams( 41 ViewGroup.LayoutParams.MATCH_PARENT, 42 ViewGroup.LayoutParams.MATCH_PARENT); 43 FrameLayout frame = new FrameLayout(this); 44 frame.setId(R.id.simple_fragment); 45 setContentView(frame, lp); 46 47 if (savedInstanceState == null) { 48 // Do first time initialization -- add fragment. 49 Fragment newFragment = new ReceiveResultFragment(); 50 FragmentTransaction ft = getFragmentManager().beginTransaction(); 51 ft.add(R.id.simple_fragment, newFragment).commit(); 52 } 53 } 54 55 public static class ReceiveResultFragment extends Fragment { 56 // Definition of the one requestCode we use for receiving resuls. 57 static final private int GET_CODE = 0; 58 59 private TextView mResults; 60 61 private OnClickListener mGetListener = new OnClickListener() { 62 public void onClick(View v) { 63 // Start the activity whose result we want to retrieve. The 64 // result will come back with request code GET_CODE. 65 Intent intent = new Intent(getActivity(), SendResult.class); 66 startActivityForResult(intent, GET_CODE); 67 } 68 }; 69 70 @Override onCreate(Bundle savedInstanceState)71 public void onCreate(Bundle savedInstanceState) { 72 super.onCreate(savedInstanceState); 73 } 74 75 @Override onSaveInstanceState(Bundle outState)76 public void onSaveInstanceState(Bundle outState) { 77 super.onSaveInstanceState(outState); 78 } 79 80 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)81 public View onCreateView(LayoutInflater inflater, ViewGroup container, 82 Bundle savedInstanceState) { 83 View v = inflater.inflate(R.layout.receive_result, container, false); 84 85 // Retrieve the TextView widget that will display results. 86 mResults = (TextView)v.findViewById(R.id.results); 87 88 // This allows us to later extend the text buffer. 89 mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE); 90 91 // Watch for button clicks. 92 Button getButton = (Button)v.findViewById(R.id.get); 93 getButton.setOnClickListener(mGetListener); 94 95 return v; 96 } 97 98 /** 99 * This method is called when the sending activity has finished, with the 100 * result it supplied. 101 */ 102 @Override onActivityResult(int requestCode, int resultCode, Intent data)103 public void onActivityResult(int requestCode, int resultCode, Intent data) { 104 // You can use the requestCode to select between multiple child 105 // activities you may have started. Here there is only one thing 106 // we launch. 107 if (requestCode == GET_CODE) { 108 109 // We will be adding to our text. 110 Editable text = (Editable)mResults.getText(); 111 112 // This is a standard resultCode that is sent back if the 113 // activity doesn't supply an explicit result. It will also 114 // be returned if the activity failed to launch. 115 if (resultCode == RESULT_CANCELED) { 116 text.append("(cancelled)"); 117 118 // Our protocol with the sending activity is that it will send 119 // text in 'data' as its result. 120 } else { 121 text.append("(okay "); 122 text.append(Integer.toString(resultCode)); 123 text.append(") "); 124 if (data != null) { 125 text.append(data.getAction()); 126 } 127 } 128 129 text.append("\n"); 130 } 131 } 132 } 133 } 134