【问题标题】:Static class to use in many activity?在许多活动中使用的静态类?
【发布时间】:2017-07-05 04:39:02
【问题描述】:

我制作了一个静态类,以便我可以在不同的活动中访问它.. 我需要将 1 个数据传递给另一个活动,但我不知道如何正确完成。 我尝试使用 put 额外的东西,但它是用于将数据传递给下一个活动(不是全局(可能))。

这是我的课..

static class AuthorizedUserIdClass
{
    private static Guid _authorizedUserId;

    static Guid AuthorizedUserId
    {
        get { return _authorizedUserId; }
        set { _authorizedUserId = value; }
    }
    static AuthorizedUserIdClass()
    {
        AuthorizedUserId = _authorizedUserId;
    }

}

对吗? 那么,如何在活动中使用它呢?以及如何在另一个activity中调用填充的数据?

提前致谢:)

【问题讨论】:

    标签: c# android visual-studio-2015 xamarin.android


    【解决方案1】:

    将额外的传递数据从一个活动转移到另一个活动。如果你想访问整个应用程序标签的值,你可以像使用全局变量一样

    1. 创建应用程序的类扩展,用于存储价值应用程序标签

       public class GlobalVariable extends Application {
      
         private String message;
      
         public String getMessage() {
            return message;
         }
      
         public void setMessage(String message) {
            this.message = message;
         }
       }
      
    2. 将 GlobalVariable 添加到 AndroidManifest.xml

      <application
        android:name=".GlobalVariable"     
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        //........ 
      </application>
      
    3. 将值设置为全局变量,如

      GlobalVariable globalVariable = ((GlobalVariable) getApplicationContext());
      globalVariable.setMessage("Hello");
      
    4. 终于从像Activity中获取价值

      GlobalVariable globalVariable = ((GlobalVariable)getApplicationContext());
      Stirng message = globalVariable.getMessage();
      

    【讨论】:

      【解决方案2】:
      public class AuthorizedUserIdClass
      {
          private static AuthorizedUserIdClass instance;
      
          public static AuthorizedUserIdClass getInstance() {
              if (instance == null) {
                  instance = new AuthorizedUserIdClass();
              }
              return instance;
          }
          private static Guid _authorizedUserId;
      
          static Guid AuthorizedUserId
          {
              get { return _authorizedUserId; }
              set { _authorizedUserId = value; }
          }
      
      }
      

      在你的活动中。

      AuthorizedUserIdClass authorizedUserIdClass = AuthorizedUserIdClass.getInstance();
      authorizedUserIdClass.set(value);
      authorizedUserIdClass.get();
      

      【讨论】:

      • AuthorizedUserIdClass authorizedUserIdClass=AuthorizedUserIdClass.getInstance();authorizedUserIdClass.set(UserInfo.ID);
      【解决方案3】:

      您可以像这样直接在活动或片段中的任何地方使用

       Guid value = AuthorizedUserIdClass._authorizedUserId;
      

      【讨论】:

        【解决方案4】:

        据我了解您的情况,我认为您想保存一些全局数据并希望在不同的活动中访问。

        所以我建议使用 Application 类。据说

        Android 中的 Application 类是 Android 应用程序中的基类,它包含所有其他组件,例如活动和服务。 Application 类或 Application 类的任何子类在创建应用程序/包的进程时在任何其他类之前实例化。

        阅读更多 herethis 是关于 SO 的最佳问题,符合您的情况以保存数据。

        请尝试一下,如果您发现任何其他问题,请告诉我

        【讨论】:

        • 你引用的文档的下一段是:"注意:通常不需要子类化应用程序。在大多数情况下,静态单例可以以更模块化的方式提供相同的功能.", developer.android.com/reference/android/app/Application.html
        • 是的,在很多情况下我们可以并且我们应该使用 Application 类作为单例
        • 注意:通常不需要继承 Application。 在大多数情况下,静态单例可以以更模块化的方式提供相同的功能
        • 谁要求子类化
        • 所以你不想扩展Application类?
        【解决方案5】:

        要在活动之间传递数据,您可以使用 Intent。试试看

        intent.putExtra(key, value);
        

        这样你就可以将它传递给另一个活动。

        除此之外,您还可以使用首选项。用户偏好是另一种保存“全局”变量和东西的方法,也比静态类更好。

        您可以在 developer.android.com 中查看如何使用这些详细信息 但是,如果您必须使用静态类来调用此变量,则可以直接调用该类。比如,你的类叫做 Util,那么你就可以调用

        Util.publicmethod();
        int c = Util.publicIntVariable
        

        【讨论】:

        • intent 填充 var intent=new intent(this, typeof("sampleActivity")); soo,只有在下一个活动中,我们才能从 extra 中获得价值?
        • 就是这样。 Intent intent = new intent(this, sampleActivity.class) intent.putExtra("someValue", "value") startActivity(intent) Then in the next activity you can just String v = getIntent().getExtras().getString("someValue") v 将是“价值”
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-01
        • 2011-10-29
        • 1970-01-01
        相关资源
        最近更新 更多