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.supportv4.app; 18 19 import com.example.android.supportv4.app.SendResult; 20 import com.example.android.supportv4.R; 21 22 import android.support.v4.app.Fragment; 23 import android.support.v4.app.FragmentActivity; 24 import android.support.v4.app.FragmentTransaction; 25 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; 36 37 public class FragmentReceiveResultSupport extends FragmentActivity { 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams( 43 ViewGroup.LayoutParams.FILL_PARENT, 44 ViewGroup.LayoutParams.FILL_PARENT); 45 FrameLayout frame = new FrameLayout(this); 46 frame.setId(R.id.simple_fragment); 47 setContentView(frame, lp); 48 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(); 54 } 55 } 56 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; 60 61 private TextView mResults; 62 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); 69 } 70 }; 71 72 @Override 73 public void onCreate(Bundle savedInstanceState) { 74 super.onCreate(savedInstanceState); 75 } 76 77 @Override 78 public void onSaveInstanceState(Bundle outState) { 79 super.onSaveInstanceState(outState); 80 } 81 82 @Override 83 public View onCreateView(LayoutInflater inflater, ViewGroup container, 84 Bundle savedInstanceState) { 85 View v = inflater.inflate(R.layout.receive_result, container, false); 86 87 // Retrieve the TextView widget that will display results. 88 mResults = (TextView)v.findViewById(R.id.results); 89 90 // This allows us to later extend the text buffer. 91 mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE); 92 93 // Watch for button clicks. 94 Button getButton = (Button)v.findViewById(R.id.get); 95 getButton.setOnClickListener(mGetListener); 96 97 return v; 98 } 99 100 /** 101 * This method is called when the sending activity has finished, with the 102 * result it supplied. 103 */ 104 @Override 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 108 // we launch. 109 if (requestCode == GET_CODE) { 110 111 // We will be adding to our text. 112 Editable text = (Editable)mResults.getText(); 113 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)"); 119 120 // Our protocol with the sending activity is that it will send 121 // text in 'data' as its result. 122 } else { 123 text.append("(okay "); 124 text.append(Integer.toString(resultCode)); 125 text.append(") "); 126 if (data != null) { 127 text.append(data.getAction()); 128 } 129 } 130 131 text.append("\n"); 132 } 133 } 134 } 135 } 136