public class XWalkInitializer
extends java.lang.Object
XWalkInitializer is an alternative to XWalkActivity with the difference
that it provides a way of initializing Crosswalk Project runtime in background silently. Another
advantage is that the developer can use their own activity class directly rather than having it
extend XWalkActivity. However, XWalkActivity is still recommended because it
makes the code simpler.
If the initialization failed, which means Crosswalk Project runtime doesn't exist or doesn't
match the application, you could use XWalkUpdater to download suitable Crosswalk Project
runtime.
Here is the sample code for embedded mode and shared mode:
import android.app.Activity;
import android.os.Bundle;
import org.xwalk.core.XWalkInitializer;
import org.xwalk.core.XWalkUpdater;
import org.xwalk.core.XWalkView;
public class MainActivity extends Activity implements
XWalkInitializer.XWalkInitListener,
XWalkUpdater.XWalkUpdateListener {
private XWalkInitializer mXWalkInitializer;
private XWalkUpdater mXWalkUpdater;
private XWalkView mXWalkView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Must call initAsync() before anything that involves the embedding
// API, including invoking setContentView() with the layout which
// holds the XWalkView object.
mXWalkInitializer = new XWalkInitializer(this, this);
mXWalkInitializer.initAsync();
// Until onXWalkInitCompleted() is invoked, you should do nothing with the
// embedding API except the following:
// 1. Instantiate the XWalkView object
// 2. Call XWalkPreferences.setValue()
// 3. Call mXWalkView.setXXClient(), e.g., setUIClient
// 4. Call mXWalkView.setXXListener(), e.g., setDownloadListener
// 5. Call mXWalkView.addJavascriptInterface()
setContentView(R.layout.activity_main);
mXWalkView = (XWalkView) findViewById(R.id.xwalkview);
}
@Override
protected void onResume() {
super.onResume();
// Try to initialize again when the user completed updating and
// returned to current activity. The initAsync() will do nothing if
// the initialization is proceeding or has already been completed.
mXWalkInitializer.initAsync();
}
@Override
public void onXWalkInitStarted() {
}
@Override
public void onXWalkInitCancelled() {
// Perform error handling here
finish();
}
@Override
public void onXWalkInitFailed() {
if (mXWalkUpdater == null) {
mXWalkUpdater = new XWalkUpdater(this, this);
}
mXWalkUpdater.updateXWalkRuntime();
}
@Override
public void onXWalkInitCompleted() {
// Do anyting with the embedding API
mXWalkView.load("https://crosswalk-project.org/", null);
}
@Override
public void onXWalkUpdateCancelled() {
// Perform error handling here
finish();
}
}
When the application was generated, some default layout resources were added to the project. Add a single XWalkView resource to a proper place in the main layout resource file, res/layout/activity_main.xml, like this:
<org.xwalk.core.XWalkView
android:id="@+id/xwalkview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
| Modifier and Type | Class and Description |
|---|---|
static interface |
XWalkInitializer.XWalkInitListener
Interface used to initialize the Crosswalk runtime.
|
| Constructor and Description |
|---|
XWalkInitializer(XWalkInitializer.XWalkInitListener listener,
android.app.Activity activity)
Create an initializer for single activity.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancelInit()
Attempt to cancel the initialization.
|
boolean |
initAsync()
Initialize the Crosswalk runtime in background asynchronously.
|
public XWalkInitializer(XWalkInitializer.XWalkInitListener listener, android.app.Activity activity)
This method must be invoked on the UI thread.
listener - The XWalkInitializer.XWalkInitListener to use.activity - The activity which initiate the initializationpublic boolean initAsync()
This method must be invoked on the UI thread.
public boolean cancelInit()