StartActivity 工具类

每个项目中都会有界面的跳转,为了使用方便,我们可以将其封装一下,可以相应的减少代码量,直接上代码

public class JumperUtils {
   public static void JumpTo(Activity activity, Class cls) {
      try {
         Intent intent = new Intent(activity, cls);
         activity.startActivity(intent);
      } catch (ActivityNotFoundException e) {
         e.printStackTrace();
      } catch (NullPointerException e) {
         e.printStackTrace();
      }        
   }
   
   
   public static void JumpTo(Activity activity, Class cls, Bundle bundle) {
      try {
         Intent intent = new Intent(activity, cls);
         intent.putExtras(bundle);
         activity.startActivity(intent);
      } catch (ActivityNotFoundException e) {
         e.printStackTrace();
      } catch (NullPointerException e) {
         e.printStackTrace();
      }        
   }
   
   /**
    * startActivityForResult,跳�?
    * @param activity
    * @param cls
    * @param bundle
    * @param requestCode
    */
   public static void JumpToForResult(Activity activity, Class cls, Bundle bundle, int requestCode) {
      try {
         Intent intent = new Intent(activity, cls);
         intent.putExtras(bundle);
         activity.startActivityForResult(intent,requestCode);
      } catch (ActivityNotFoundException e) {
         e.printStackTrace();
      } catch (NullPointerException e) {
         e.printStackTrace();
      }        
   }
   
   /**
    * startActivityForResult,跳�?
    * @param activity
    * @param cls
    * @parambundle
    * @param requestCode
    */
   public static void JumpToForResult(Activity activity, Class cls, int requestCode) {
      try {
         Intent intent = new Intent(activity, cls);
         activity.startActivityForResult(intent,requestCode);
      } catch (ActivityNotFoundException e) {
         e.printStackTrace();
      } catch (NullPointerException e) {
         e.printStackTrace();
      }        
   }
}
有需要的可以直接拿去使用

你可能感兴趣的:(StartActivity 工具类)