java - 如何创建全局 "Intent explicitIntent = new Intent(...)"方法

标签 java android

我在不同的类( View )中具有相同的方法,并且希望以某种方式抽象它们,因此我在多个类中没有相同的代码部分。我该如何在 Android 中执行此操作?

我在项目中多次使用的方法如下所示:

    public void goHome() {
    Intent explicitIntent = new Intent(CurrentClass.this, ClassWhereToGo.class);
    startActivity(explicitIntent);
}

最后我只想在整个项目中拥有这个,并且能够从不同的 View 动态访问它。我想我必须向 goHome(p1, p2) 添加两个参数,以便能够从各种 View 访问该方法。我只是不知道该方法应该放在哪里以及如何让它与参数一起工作。

我认为理想情况下我会创建某种帮助器类?

感谢您的帮助!

最佳答案

public class Helper
{

    public static void goHome(Context context, Class<?> cls) {
        Intent explicitIntent = new Intent(context, cls);
        context.startActivity(explicitIntent);
    }

}

它的使用方式如下:

Helper.goHome(CurrentClass.this, ClassWhereToGo.class);

关于java - 如何创建全局 "Intent explicitIntent = new Intent(...)"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7864658/

相关文章:

java - 为什么我在 java 应用程序中收到 ClassNotFoundException?

eclipse - 打开 eclipse 时无法识别的 vm 选项

java - Jboss AS 6.1.0 与 Java7 的握手超时

java - jackson json 与 hibernate : jackson-module-hibernate usage

java - BitmapFactory.decodeFile 返回 null

android Notification - 如何不在前台显示

Android ViewPager 布局的不同宽度

android - android中的多线程

javascript - 如何使用ajax将表单数据发送到Spring Controller

android - 在 Android Studio 中导入/刷新 Gradle 项目时出现问题