如何从另一个类调用 Activity 类的方法?

huangapple go评论82阅读模式
英文:

How do I call Activity class method from another class?

问题

Config.Java //Singleton class

// 配置类
public class Config
{
  public String name = ""; // 名称
  
  public void getSettingsFromServer()
  {
    name = ""; // 从服务器获取的名称
  }    
}

MyActivity.java

// 我的活动类
public class SettingsSystem extends AppCompatActivity
{
  private Config config = Config.getInstance();
  
  protected void onCreate (Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_system);
    ApplySettings();
  }
  
  public void ApplySettings()
  {
    // 在此处应用设置
  }    
}

这两个类分别位于不同的文件中。

英文:

I have a Config class which receives the settings from server. These settings are then applied in an activity method named ApplySettings(). I called this ApplySettings() function onCreate function. first time it worked fine but I could not detect setting change in Activity class. I want to call this ApplySettings() method from Config class as soon as settings change there. How can I do this? Thanks

Config.Java //Singleton class
public class Config
{
  public String name = "";
  public void getSettingsFromServer()
  {
    name = "";//name received from server
  }    
}




MyActivity.java
public class SettingsSystem extends AppCompatActivity
{
  private Config config = Config.getInstance();
  protected void onCreate (Bundle savedInstanceState)
  {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_settings_system);
    ApplySettings();
  }
  public void ApplySettings()
  {
    //settings are applied here
  }    
}

Both classes are in separate files.

答案1

得分: 1

你会想要将MyActivity的实例存储在你的配置类内。

Config.java中添加一个方法:

public class Config
{
  public String name = "";
  private SettingsSystem activity;
  public void getSettingsFromServer()
  {
    name = "";//name received from server
  }

  public void setMyActivity(SettingsSystem activity) 
  {
    this.activity = activity;
  }

  public void applyChanges()
  {
    if (this.activity != null)
    {
      this.activity.ApplySettings();
    }
  }    
}

然后在你的MyActivity类中可以这样做:

public void setConfigActivity() 
{
  this.config.setMyActivity(this);
}

现在你的配置实例拥有了SettingsSystem

注意:

方法不应以大写字母开头,这违反了Java编码标准。另外,你称其为MyActivity.java,但显示为SettingsSystem,在这里需要保持一致,因为不一致会导致无法编译。

此外,具有循环依赖关系是不良实践。可能需要重新考虑这两个类之间的交互方式。

英文:

You would want to store the instance of MyActivity inside your config class.

Add a method in Config.java:

public class Config
{
  public String name = "";
  private SettingsSystem activity;
  public void getSettingsFromServer()
  {
    name = "";//name received from server
  }

  public void setMyActivity(SettingsSystem activity) 
  {
    this.activity = activity;
  }

  public void applyChanges()
  {
    if (this.activity != null)
    {
      this.activity.ApplySettings();
    }
  }    
}

Then in your MyActivity class you can do this:

public void setConfigActivity() 
{
  this.config.setMyActivity(this);
}

Now your Config Instance has SettingsSystem.

NOTE

Methods shouldn't begin with Capital letter. This is against Java coding standard. Also you say its MyActivity.java but show it as SettingsSystem Would need consistency here as that will not compile.

Also it is bad practice to have circular dependencies. Might want to rethink how the two classes interact with each other.

答案2

得分: 1

你可以使用EventBus来实现你想要的功能。

  1. 创建一个名为SettingsChangedEvent的新类:

    final public class SettingsChangedEvent { /* 如果需要的话,添加额外的字段 */ }
    
  2. 将下面的代码添加到Config类的getSettingsFromServer()方法中:

    public void getSettingsFromServer() {
        name = ""; // 从服务器接收到的名称
        // [添加此部分] 通知活动设置已更改,
        // 应用新的设置
        EventBus.getDefault().postSticky(new SettingsChangedEvent());
    }
    
  3. SettingsSystem活动中添加以下方法:

    @Override
    protected void onStart() {
        super.onStart();
        // 允许此活动监听设置更改事件
        EventBus.getDefault().register(this);
    }
    
    @Override
    protected void onStop() {
        // 禁止此活动监听设置更改事件
        EventBus.getDefault().unregister(this);
        super.onStop();
    }
    
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onSettingsChangedEvent(SettingsChangedEvent ev) {
        // 当从Config类通知/发布设置更改事件时,
        // 将执行此方法中的代码。
        SettingsChangedEvent event = EventBus.getDefault().removeStickyEvent(SettingsChangedEvent.class);
        if (event != null) {
            // 在这里应用新的设置
            ApplySettings();
        }
    }
    
英文:

You can use EventBus to achieve what you want.

1.Create a new class named SettingsChangedEvent

final public class SettingsChangedEvent { /* Additional fields if needed */ }

2.Add below code to getSettingsFromServer() method inside Config class

public void getSettingsFromServer() {
    name = ""; //name received from server
    // [ADD THIS] Notify to activity that settings are changed, 
    // it should apply new settings
    EventBus.getDefault().postSticky(new SettingsChangedEvent());
}

3.Add these methods to SettingsSystem activity

@Override
protected void onStart() {
    super.onStart();
    // Allow this activity to listen settings changed event
    EventBus.getDefault().register(this);
}

@Override
protected void onStop() {
    // Disallow this activity from listening settings changed event
    EventBus.getDefault().unregister(this);
    super.onStop();
}

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onSettingsChangedEvent(SettingsChangedEvent ev) {
    // When settings changed event is notified/posted from Config class, 
    // the code inside this method will be executed.
    SettingsChangedEvent event = EventBus.getDefault().removeStickyEvent(SettingsChangedEvent.class);
    if (event != null) {
        // Apply new settings here
        ApplySettings();
    }
}

huangapple
  • 本文由 发表于 2020年10月23日 01:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64487126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定