Android How to - Start Activity by class








Question

We would like to know how to start Activity by class.

Answer

/*from w  w  w.  j  av  a2s  .  com*/
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;

class ActivityUtil {
  public static void noAutoSoftInput(Activity context) {
    context.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  }

  public static void setSoftInputAdjustPan(Activity context) {
    context.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  }
  public static Boolean startActivityByClass(Context context, String packName,
      String className, Bundle bundle) {
    Intent mIntent = new Intent();
    mIntent.setClassName(packName, className);
    if (bundle != null) {
      mIntent.putExtras(bundle);
    }
    try {
      context.startActivity(mIntent);
      return true;
    } catch (Exception wException) {
      return false;
    }
  }
  public static Boolean startActivityByClass(Context context, String className) {
    return startActivityByClass(context, context.getPackageName(), className,
        null);
  }
}