How to create our own Listener interface in android?

前端 未结 9 1905
不知归路
不知归路 2020-11-22 04:55

Could someone help me to create user defined listener interface with some code snippets?

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 05:32

    I have done it something like below for sending my model class from the Second Activity to First Activity. I used LiveData to achieve this, with the help of answers from Rupesh and TheCodeFather.

    Second Activity

    public static MutableLiveData getLiveSong() {
            MutableLiveData result = new MutableLiveData<>();
            result.setValue(liveSong);
            return result;
        }
    

    "liveSong" is AudioListModel declared globally

    Call this method in the First Activity

    PlayerActivity.getLiveSong().observe(this, new Observer() {
                @Override
                public void onChanged(AudioListModel audioListModel) {
                    if (PlayerActivity.mediaPlayer != null && PlayerActivity.mediaPlayer.isPlaying()) {
                        Log.d("LiveSong--->Changes-->", audioListModel.getSongName());
                    }
                }
            });
    

    May this help for new explorers like me.

提交回复
热议问题