首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问其他java类的片段

访问其他java类的片段
EN

Stack Overflow用户
提问于 2016-01-21 06:04:16
回答 2查看 2.3K关注 0票数 1

我一直在网上搜索,但没有得到我所需要的正确答案。我是Android的新手,所以请提供一个详细的答案。

我有一个片段,它从用户那里获取一些数据,例如:名称、职业等等。每次我得到这些数据时,我都想用GSON存储到sharedPreferences。在搜索的基础上,我需要构造一个常规的Java类并传递对象(Android -将对象保存到SharedPreferences中,并在应用程序中的任何位置获取它。)。

  1. 因此,我的问题是,片段可以访问其他常规的java类函数吗? 类UserFragment扩展片段{ //从editText用户newUser =新用户(“Tom”);newUser.setOccupation(“程序员”);//等}获取数据。
  2. 片段可以实例化类并设置变量吗?
  3. 如果是1和2,我们在哪里添加用户类java文件,与另一个文件夹的片段实现相同的文件,然后包括它?

我只是提供了一个用户作为一个例子,但一般地我想知道1和2的答案。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-21 06:19:43

  1. 是的,片段对象可以实例化另一个类对象。
  2. 是的,它还可以在该对象上设置变量和调用函数。
  3. 完全由你决定。如果这个类对于片段类来说是很小且非常本地的,那么您可以在片段类中创建它。如果您希望在其他地方可以访问它,或者使它更加模块化,您可以使用不同的包名或相同的包名创建它。
票数 0
EN

Stack Overflow用户

发布于 2016-01-21 07:29:37

下面是您的问题的解决方案和澄清。首先,我想澄清这个片段是一个具有生命周期方法的类。我所说的生命周期方法是指根据应用程序或活动生命周期调用方法,就像在前台、可见和背景中一样。下面是你的问题的答案。

  1. 如果有对类的引用,则可以从类访问常规方法。 公共类YourFragment扩展了片段{@覆盖公共onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);MyClass类=新的MyClass();}}@覆盖公共空洞onStart() { super.onStart();String data = retrieveData();} String retrieveData() {返回数据;}}
  2. 是的,一个片段可以实例化一个类并设置变量,如果您有一个引用的话。
  3. 好的,现在是主要部分。您可以在生命周期方法中的任何地方调用一个方法并实例化一个类。但是我建议在onCreate()方法中实例化一个对象,或者在它之上的任何生命周期方法中实例化一个对象,比如onAttach()。这是因为以下两点。
代码语言:javascript
复制
- If you are not aware, Creating an object by calling new MyClass() takes up resources and also the object creation process is time consuming. So if you call them inside onResume() or onStart() method those objects will be created when the fragment move from visible state to focused(foreground - onResume() is called) and from background to visible(onStart() is called) state .
- This happens often when another application comes into focus(which causes the fragment to call onResume() or onStart() when your application comes into foreground) or when our application moves from background to visible state(onStart() is called). So object is created everytime this happens.
- But on the other hand if you create it inside onCreate() or lifecycle methods above it the objects will be created only when the fragment is destroyed and recreated.
- Also if you want to call methods from the class you have instantiated by above method you can call it from any lifecycle  method below the onCreate() like in onStart() (if you wanted to show results before the user uses your fragment) or in onResume() (if you wanted to show results after the user sees it) it is upto you to do this.
- Also in my example above i have called retrieveData() method from onStart() and i have wrote that method from outside the lifecycle methods. it is just for an example. You can do like that. But you have to call those methods inside the lifecycle methods according to your needs. 
- Finally here is a link to learn more about the lifecycle methods of the Fragment. [Learn About Fragment LifeCycle](https://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/)

我希望这能帮你找到解决办法。谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34916460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档