How to register Android onBackPressed pressed?

103 Views Asked by At

Project structure

- configs
- dist
- node_modules
- platforms
  - android
- plugins
- src
- web

I'am new to android, where should snippet be saved given java code is meant to be paste somewhere inside android dir. where android dir have all the necessary file to launch the app in android.

In Android

public void onBackPressed(){
  Map<String,Object> params=new HashMap<>();
  params.put("name","returnmsg");
  mWXSDKInstance.fireGlobalEventCallback("androidback",params);
}

In weex

 globalEvent.addEventListener('androidback', function (e) {
  // 这里就可以做返回事件操作了,如返回上一页或退出应用
  // that.$router.go(-1)
  // weex.requireModule('close').closeApp()
})

Suspected files inside Android dir:

suspect files For further reading please refer weex controls Android return key solution

1

There are 1 best solutions below

0
Vimm Rana On

Simply, onBackPressed is an event, which will reside in AbsWeexActivity.java file and globalEvent.addEventListener can be attach with suitable Weex lifecycle.

This is how event from android to weex could be attached and listen to.