在MainActivity中初始化对象,然后全局访问它

时间:2018-09-06 14:27:22

标签: java android

我想在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;的对象,但是有没有更好/更有效的方法呢?

2 个答案:

答案 0 :(得分:2)

这不是对Activity中的数据进行某些操作的好主意。 我建议您看一下:https://android-developers.googleblog.com/2018/08/google-releases-source-for-google-io.html

有几种选择:

  1. 将数据加载到ViewModel中,并将单个ViewModel用于多个片段。
  2. 将数据保存在某个地方(例如数据库),然后从那里加载每个屏幕(documentation
  3. 使用Rx为您存储库中某处的多个订户共享下载的数据。
  4. 使用Gson或Parcelable对对象进行序列化并通过intent发送。

我认为,第三种是最好的。

答案 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。设置/获取活动中的变量

在活动和片段中,您可以像这样getset变量:

// initialise your user variable
// set description variable
((myUser) this.getApplication()).setDescription("your description");

// get description variable
String s = ((myUser) this.getApplication()).getDescription();

希望对您有帮助。