简体   繁体   中英

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

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 .

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. That should be done using an intent.

There are several things you could do:

  1. Move your update method in another class

OR

  1. declare your update method as static and use it like this:

    MainActivity.update(msg100Preselection) ;

Try using a callbackListener:- In your 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-

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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