Atlas插件bundle与host通信之接口方式

在公共library中定义中间件接口

package cn.appblog.example_common.middleware;

public interface ISwitchMainTabListener {
    void onSwitchTab(int position);
}

单例封装接口监听器的Getter与Setter

package cn.appblog.example_common.middleware;

import android.content.Context;

public class MiddlewareHelper {
    private static MiddlewareHelper mMiddlewareHelper;
    private Context mContext;
    private ISwitchMainTabListener mSwitchMainTabListener;

    private MiddlewareHelper(Context context) {
        mContext = context;
    }

    public static MiddlewareHelper getInstance(Context context) {
        if (mMiddlewareHelper == null) {
            synchronized (MiddlewareHelper.class) {
                if (mMiddlewareHelper == null) {
                    mMiddlewareHelper = new MiddlewareHelper(context.getApplicationContext());
                }
            }
        }
        return mMiddlewareHelper;
    }

    public ISwitchMainTabListener getSwitchMainTabListener() {
        return mSwitchMainTabListener;
    }

    public void setSwitchMainTabListener(ISwitchMainTabListener listener) {
        this.mSwitchMainTabListener = listener;
    }
}

在host中设置监听

public class MainActivity extends AppCompatActivity implements ISwitchMainTabListener {

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

        MiddlewareHelper.getInstance(this).setSwitchMainTabListener(this);
    }

    @Override
    public void onSwitchTab(int position) {
        mViewPager.setCurrentItem(position, false);
    }   
}

在bundle中发送消息

MiddlewareHelper.getInstance(context).getSwitchMainTabListener().onSwitchTab(0);

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/interface-method-for-communication-between-atlas-plugin-bundle-and-host/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Atlas插件bundle与host通信之接口方式
在公共library中定义中间件接口 package cn.appblog.example_common.middleware; public interface ISwitchMainTabListener { void onSwitchTab(int posi……
<<上一篇
下一篇>>
文章目录
关闭
目 录