Android学习之五种界面通信方法总结

界面通信相信大家都很熟悉,近来在工作中也遇到各种各样的界面通信问题,今天以此总结之!

界面通信,就是Activity之间的数据传递,(Fragment也属于Activity)

通信方式1:startActivity()

    public void toBindActivity(Activity act, Bundle bundle) {
        startActivity(act, BindActivity.class, bundle);
    }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

在跳转的同时传值

接收:

int position = getIntent().getExtras().getInt("position");
  • 1
  • 1

通信方式2:创建public方法

public UserModel getUserInfo(){
        return usermodel;
    }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

接收:

UserModel model=new MainActivity().getUserInfo();
  • 1
  • 1

通信方式3:回调接口

创建接口

public interface FragmentCallBack {
    ThirdModel getThirdModel();
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

实现接口

public class BindActivity extends BaseActivity implements FragmentCallBack
{
 @Override
    public ThirdModel getThirdModel() {
        return thirdModel;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

接收:

 private ThirdModel thirdModel;
 private FragmentCallBack callBack;

  @Override
    protected void initData() {
        thirdModel = callBack.getThirdModel();

 @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        callBack = (BindActivity) activity;

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

通信方式4: 广播实现通信

广播的注册分为动态和静态两种,具体注册不在介绍。

public class DemoReceiver extends BroadcastReceiver {
    private LogUtil log = new LogUtil(getClass());

    @Override
    public void onReceive(Context context, Intent intent) {
        //过滤广播类型
        if (intent.getAction().equals("") {
          Intent intents=new Intent(context, MainActivity.class);
            Bundle bundle=new Bundle();
            bundle.putSerializable("key","value");
            intents.putExtras(bundle);
            context.startActivity(intents);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

接收:

int position = getIntent().getExtras().getInt("position");
  • 1
  • 1

通信方式5:EventBus通信

EventBus注册销毁不在讲述

发送:

EventBus.getDefault().post(new ClickEvent(ClickEvent.Type.MOVE_LOGIN, null, 3));
  • 1
  • 1

ClickEvent

public class ClickEvent {
    public enum Type {}
    public Type type;
    public View view;
    public Object data;

    public ClickEvent(Type type, View view, Object data) {
        this.type = type;
        this.view = view;
        this.data = data;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

接收

  @Override
    public void onEventMainThread(final ClickEvent event) {
        switch (event.type) {
            case RECOMMEND_NEWS:
            case LOGINOUT:
                getWindow().getDecorView().post(new Runnable() {
                    @Override
                    public void run() {
                        adapter.selectTab((Integer) event.data);
                    }
                });
                break;
            case MOVE_LOGIN:
                RouteManager.getInstance().toLogin(this, (Integer) event.data);
                finish();
                break;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

注意:EventBus通信必须是活动在堆栈中已经存在,否则会接收不到,EventBus也可以实现在适配器与活动的通信。

posted @ 2016-11-11 22:03  天涯海角路  阅读(369)  评论(0)    收藏  举报