简体   繁体   English

从非活动 class 调用 MainActivity class 的方法

[英]Call a method from MainActivity class from a non-activity class

I have troubles on calling the method update from MainActivity class in a the MSG0100 non-activity class我在从MSG0100非活动 class 中的MainActivity class 调用方法更新时遇到麻烦

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void update(boolean msg100Preselection){
    if(msg100Preselection){
        mExpandableListViewAdapter.setSelectedChild(-1);
        mExpandableListViewAdapter.notifyDataSetChanged();
    }
}
}

And this is my class where i want to call the update method of Mainactivity .这是我的 class ,我想在其中调用Mainactivity的更新方法。

public class MSG0100{
   boolean msg100Preselection=false;
 pulic void onUpdate(){
     msg100Preselection=true; 
     // Want to call my update method here
     MainActivity activity= new MainActivity();
     activity.update(msg100Preselection); //<-------- Using mainactiviy object crashes my app.
 }
}

What you want is impossible as you dont have a pointer to your main activity.你想要什么是不可能的,因为你没有指向你的主要活动的指针。

The following statement is invalid.以下陈述无效。

  MainActivity activity= new MainActivity();

You are not allowed to use the new operator to create an activity.不允许使用new运算符创建活动。 That should be done using an intent.这应该使用意图来完成。

There are several things you could do:你可以做几件事:

  1. Move your update method in another class将您的更新方法移到另一个 class

OR或者

  1. declare your update method as static and use it like this:将您的更新方法声明为static并像这样使用它:

    MainActivity.update(msg100Preselection) ; MainActivity.update(msg100Preselection) ;

Try using a callbackListener:- In your MSG0100 class尝试使用回调侦听器:- 在您的 MSG0100 class

    public class MSG0100 {
        boolean msg100Preselection = false;
        private static OnUpdateListener mListener;

        public static setListener(OnUpdateListener mListener) {
            this.mListener = mListener;
        }

        public void onUpdate() {
            msg100Preselection = true;
            if (mListener != null)
                mListener.onUpdate(msg100Preselection);
        }

        public interface OnUpdateListener()

        {
            void onUpdate ( boolean msg100Preselection);
        }
    }

In your MainActivity-在你的 MainActivity-

public class MainActivity extends AppCompatActivity, OnUpdateListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MSG0100.setListener(this)
    }

    @Override
    public void onUpdate(boolean msg100Preselection) {
        if (msg100Preselection) {
            mExpandableListViewAdapter.setSelectedChild(-1);
            mExpandableListViewAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MSG0100.setListener(null)
    }
}

This way you won't have any memory leaks or crashes due to Activity being killed.这样您就不会因为 Activity 被杀死而导致任何 memory 泄漏或崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM