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