在 Android 的不同 Activity 中重用方法

发布于 2024-12-07 18:56:31 字数 853 浏览 3 评论 0原文

是否可以在不同的活动中重用方法?举例来说,我在 StudentActivity 中有 retrieveAllStudents()。我可以将其设置为静态或其他内容并调用 ClassActivity 中的方法吗?或者我是否需要在这两个活动中重复该方法?

哪一个是正确的?

示例 1:

StudentActivity

public static ArrayList<Student> retrieveAllStudents(){
    ...
    return studentList;
}

ClassActivity

import StudentActivity

ArrayList<Student> studentList= StudentActivity.retrieveAllStudents();

示例 2:

StudentActivity

public static ArrayList<Student> retrieveAllStudents(){
    ...
    return studentList;
}

ClassActivity

public static ArrayList<Student> retrieveAllStudents(){
    ...
    return studentList;
}

ArrayList<Student> studentList= retrieveAllStudents();

Is it possible to reuse methods in different activities? Say for example, I have retrieveAllStudents() in StudentActivity. Can I make it static or something and call the method in ClassActivity? Or do I need to duplicate the method in both activities?

Which one is correct?

Example 1:

StudentActivity

public static ArrayList<Student> retrieveAllStudents(){
    ...
    return studentList;
}

ClassActivity

import StudentActivity

ArrayList<Student> studentList= StudentActivity.retrieveAllStudents();

Example 2:

StudentActivity

public static ArrayList<Student> retrieveAllStudents(){
    ...
    return studentList;
}

ClassActivity

public static ArrayList<Student> retrieveAllStudents(){
    ...
    return studentList;
}

ArrayList<Student> studentList= retrieveAllStudents();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安穩 2024-12-14 18:56:31

如果它是公共静态的,那么它肯定可以从其他活动(以及应用程序中的任何其他类)访问。但是,当活动相互调用函数时,可能会导致代码过于复杂。考虑将 getStudent() 函数和其他共享功能移至单独的 Student 类。

编辑是的,可以重用其他类中的方法。这是很常见的并且被认为是最佳实践。鉴于你的两个例子,第一个更正确。

If it is public static it is definitely accesible from other activities (and any other class in your app). However, when activities call functions on each other, it can lead to overly complex code. Consider moving the getStudent() function and other shared functionality to a separate Student class.

Edit yes it is possible to reuse methods in other classes. This is very common and considered a best practice. Given your two examples, the first is more correct.

余生共白头 2024-12-14 18:56:31

如果是泛型方法,则将其保留在Application类中,创建应用程序类的静态上下文

public static Context getAppContext() {
返回上下文;
};

If it is a generic method, then keep it in Application class, make static context of application class

public static Context getAppContext() {
return context;
};

泪之魂 2024-12-14 18:56:31

我认为更好的方法是使用必要的方法创建类(只是类而不是 Activity),然后在 Activity 中创建该常用方法类的实例,并在需要时使用方法。 (示例

其他解决方案是从 ClassActivity 扩展 StudentActivity,如果它们都需要是 Activity,但此处情况并非如此。

不管怎样,也许你应该再次考虑哪些类应该是你的应用程序中的活动。
阅读一些有关活动的信息可能会有所帮助。
那么我建议根据您的需要使用上述两种方法之一。这将防止 Android 系统从堆栈中删除 Activity 引起的问题(您可以阅读有关 Activity 生命周期也是如此)。

干杯

By my opinion better approach would be to make class (just class but not Activity) with necessary methods, then create instance of that common methods class in Activity and use methods when u need them. (example)

Other solution would be to extend StudentActivity from ClassActivity if they both need to be Activities which is not the case here.

Anyway maybe you should think again what classes should be Activities in your App.
It might be useful to read a bit about activities.
Then I propose using one of two methods described above according to your needs. That will prevent problems caused by removing your activity from stack by Android system (you can read about activity lifecycle too) .

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文