launch Activity and close current Activity - Android android.app

Android examples for android.app:Activity Navigation

Description

launch Activity and close current Activity

Demo Code

import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Main {
  public static void launchActivity(Activity context, Class<? extends Activity> activity,
      boolean closeCurrentActivity) {
    launchActivity(context, activity, closeCurrentActivity, null);
  }//from ww  w .j a  v  a 2s.c  om

  /**
   * Launch an Activity.
   *
   * @param context
   *          The current Context or Activity that this method is called from.
   * @param activity
   *          The new Activity to open.
   * @param closeCurrentActivity
   *          whether or not the current activity should close.
   * @param params
   *          Parameters to add to the intent as a Bundle.
   */
  public static void launchActivity(Activity context, Class<? extends Activity> activity, boolean closeCurrentActivity,
      Map<String, String> params) {
    Intent intent = new Intent(context, activity);

    if (params != null) {
      Bundle bundle = new Bundle();
      for (Entry<String, String> param : params.entrySet()) {
        bundle.putString(param.getKey(), param.getValue());
      }
      intent.putExtras(bundle);
    }

    context.startActivity(intent);
    if (closeCurrentActivity) {
      context.finish();
    }
  }

}

Related Tutorials