我在不同的类( 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/