1 package com.android.quake; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 6 /** 7 * Usage: 8 * 9 * Intent intent = PreconditionActivityHelper.createPreconditionIntent( 10 * activity, WaitActivity.class); 11 * // Optionally add extras to pass arguments to the intent 12 * intent.putExtra(Utils.EXTRA_ACCOUNT, account); 13 * PreconditionActivityHelper.startPreconditionActivityAndFinish(this, intent); 14 * 15 * // And in the wait activity: 16 * PreconditionActivityHelper.startOriginalActivityAndFinish(this); 17 * 18 */ 19 20 public class PreconditionActivityHelper { 21 /** 22 * Create a precondition activity intent. 23 * @param activity the original activity 24 * @param preconditionActivityClazz the precondition activity's class 25 * @return an intent which will launch the precondition activity. 26 */ createPreconditionIntent(Activity activity, Class preconditionActivityClazz)27 public static Intent createPreconditionIntent(Activity activity, 28 Class preconditionActivityClazz) { 29 Intent newIntent = new Intent(); 30 newIntent.setClass(activity, preconditionActivityClazz); 31 newIntent.putExtra(EXTRA_WRAPPED_INTENT, activity.getIntent()); 32 return newIntent; 33 } 34 35 /** 36 * Start the precondition activity using a given intent, which should 37 * have been created by calling createPreconditionIntent. 38 * @param activity 39 * @param intent 40 */ startPreconditionActivityAndFinish(Activity activity, Intent intent)41 public static void startPreconditionActivityAndFinish(Activity activity, 42 Intent intent) { 43 activity.startActivity(intent); 44 activity.finish(); 45 } 46 47 /** 48 * Start the original activity, and finish the precondition activity. 49 * @param preconditionActivity 50 */ startOriginalActivityAndFinish( Activity preconditionActivity)51 public static void startOriginalActivityAndFinish( 52 Activity preconditionActivity) { 53 preconditionActivity.startActivity( 54 (Intent) preconditionActivity.getIntent() 55 .getParcelableExtra(EXTRA_WRAPPED_INTENT)); 56 preconditionActivity.finish(); 57 } 58 59 static private final String EXTRA_WRAPPED_INTENT = 60 "PreconditionActivityHelper_wrappedIntent"; 61 } 62