I looked for the XWalkView.java code so I can make something useful but it is not published yet. But there is at least a good solution that can work:
Create for the first time the XWalkView instance inside an activity. then find a way to store it in a service and reuse that same instance whenever your activity connects to the service (this way, your html and js are not reloaded ;) )
After googling, I understood that the XWalkView instance needed and activity so it registers some life cycle listeners. so when the activity is destroyed for example the XwalkView.onDestroy method is called (so I had to disable it in my case to keep the same instance and reuse it)
Here is My simple example:
MainActivity.Java
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.MutableContextWrapper;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import java.util.logging.Logger;
import org.xwalk.core.XWalkView;
public class MainActivity extends Activity implements ServiceConnection {
private Logger logger = Logger.getLogger("com.tr");
private XWalkView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, MyService.class));
bindService(new Intent(this, MyService.class), this, BIND_AUTO_CREATE);
}
@Override
public void setContentView(View view) {
final ViewParent parent = view.getParent();
if (parent != null) {
ViewGroup group = (ViewGroup) parent;
group.removeView(view);
}
super.setContentView(view);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private boolean bound;
@Override
protected void onStop() {
super.onStop();
if (bound) {
unbindService(this);
bound = false;
}
}
public void onServiceConnected(ComponentName name, IBinder s) {
bound = true;
MyService.MyBinder binder = (MyService.MyBinder) s;
if (binder.getView() != null) {
view = binder.getView();
((MutableContextWrapper) view.getContext()).setBaseContext(this);
view.onShow();
} else {
view = new XWalkView(new MutableContextWrapper(this), this) {
@Override
public void onDestroy() {
// super.onDestroy();
//disable this method to keep an insatce in memory
}
};
view.load("http://10.110.23.198:8080/mdl/templates/android-dot-com/", null);
binder.setView(view);
}
setContentView(view);
}
public void onServiceDisconnected(ComponentName name) {
}
}
The service class
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import org.xwalk.core.XWalkView;
/**
*
* @author Ramdane
*/
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder(this);
}
public class MyBinder extends Binder {
private MyService service;
private XWalkView view;
public MyBinder(MyService service) {
this.service = service;
}
public MyBinder() {
}
public void setService(MyService service) {
this.service = service;
}
public MyService getService() {
return service;
}
public XWalkView getView() {
return view;
}
public void setView(XWalkView view) {
this.view = view;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
}
I hope it will work for you.