1 /* 2 * Copyright (C) 2007 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 // Need the following import to get access to the app resources, since this 20 // class is in a sub-package. 21 import com.example.android.apis.R; 22 23 import android.app.Activity; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.widget.Button; 29 30 31 /** 32 * <p>Example of removing yourself from the history stack after forwarding to 33 * another activity. This can be useful, for example, to implement 34 a confirmation dialog before the user goes on to another activity -- once the 35 user has confirmed this operation, they should not see the dialog again if they 36 go back from it.</p> 37 38 <p>Note that another way to implement a confirmation dialog would be as 39 an activity that returns a result to its caller. Either approach can be 40 useful depending on how it makes sense to structure the application.</p> 41 42 <h4>Demo</h4> 43 App/Activity/Receive Result 44 45 <h4>Source files</h4> 46 <table class="LinkTable"> 47 <tr> 48 <td class="LinkColumn">src/com.example.android.apis/app/Forwarding.java</td> 49 <td class="DescrColumn">Forwards the user to another activity when its button is pressed</td> 50 </tr> 51 <tr> 52 <td class="LinkColumn">/res/any/layout/forwarding.xml</td> 53 <td class="DescrColumn">Defines contents of the Forwarding screen</td> 54 </tr> 55 </table> 56 */ 57 public class Forwarding extends Activity 58 { 59 @Override 60 protected void onCreate(Bundle savedInstanceState) 61 { 62 super.onCreate(savedInstanceState); 63 64 setContentView(R.layout.forwarding); 65 66 // Watch for button clicks. 67 Button goButton = (Button)findViewById(R.id.go); 68 goButton.setOnClickListener(mGoListener); 69 } 70 71 private OnClickListener mGoListener = new OnClickListener() 72 { 73 public void onClick(View v) 74 { 75 // Here we start the next activity, and then call finish() 76 // so that our own will stop running and be removed from the 77 // history stack. 78 Intent intent = new Intent(); 79 intent.setClass(Forwarding.this, ForwardTarget.class); 80 startActivity(intent); 81 finish(); 82 } 83 }; 84 } 85 86