我想在MainActivity中创建一个对象,但是我想全局访问它,所以假设我有此类
public class User {
private String description;
private String display_name;
private String email;
private String followers;
private String following;
private String posts;
private String profile_photo;
private String user_id;
private String username;
public User(){}
public User(String description, String display_name, String email, String followers, String following, String posts, String profile_photo, String user_id, String username) {
this.description = description;
this.display_name = display_name;
this.email = email;
this.followers = followers;
this.following = following;
this.posts = posts;
this.profile_photo = profile_photo;
this.user_id = user_id;
this.username = username;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDisplay_name() {
return display_name;
}
public void setDisplay_name(String display_name) {
this.display_name = display_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFollowers() {
return followers;
}
public void setFollowers(String followers) {
this.followers = followers;
}
public String getFollowing() {
return following;
}
public void setFollowing(String following) {
this.following = following;
}
public String getPosts() {
return posts;
}
public void setPosts(String posts) {
this.posts = posts;
}
public String getProfile_photo() {
return profile_photo;
}
public void setProfile_photo(String profile_photo) {
this.profile_photo = profile_photo;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"description='" + description + '\'' +
", display_name='" + display_name + '\'' +
", email='" + email + '\'' +
", followers='" + followers + '\'' +
", following='" + following + '\'' +
", posts='" + posts + '\'' +
", profile_photo='" + profile_photo + '\'' +
", user_id='" + user_id + '\'' +
", username='" + username + '\'' +
'}';
}
}
并且填充所有参数的方法在MainActivity中
public static User currentUser;
private void setupFirebase() {
Log.e(TAG, "setupFirebase: Setting up firebase");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.e(TAG, "onDataChange: Retrieving data.....");
FirebaseMethods firebaseMethods = new FirebaseMethods(mContext);
currentUser = new User();
currentUser = firebaseMethods.getUserSettings(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
现在我有多个活动和片段,如何在其他地方访问此currentUser
对象,我知道我可以使用类似currentUser = MainActivity.currentUser;
的对象,但是有没有更好/更有效的方法呢?
答案 0 :(得分:2)
这不是对Activity中的数据进行某些操作的好主意。 我建议您看一下:https://android-developers.googleblog.com/2018/08/google-releases-source-for-google-io.html
有几种选择:
我认为,第三种是最好的。
答案 1 :(得分:2)
我建议您执行以下步骤。您可以在应用程序的生命周期中创建全局变量keep keep值,而不管运行哪个活动。
1。扩展基本的应用程序类
扩展基类android.app.Application
并添加成员变量,如下所示:
public class myUser extends Application {
private String description;
...
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
...
}
2。在清单中声明:
在您的Android manifest
中,您必须声明实现android.app.Application
的类(将android:name=".myUser"
属性添加到现有的应用程序标签中):
<application
android:name=".myUser"
android:icon="@drawable/my_icon"
android:label="@string/app_name">
3。设置/获取活动中的变量
在活动和片段中,您可以像这样get
和set
变量:
// initialise your user variable
// set description variable
((myUser) this.getApplication()).setDescription("your description");
// get description variable
String s = ((myUser) this.getApplication()).getDescription();
希望对您有帮助。