Get Off My Lawn Get Off My Lawn - 7 days ago 4
Java Question

Android activity doesn't close completely

I have a javascript interface that I am using with crosswalk, and it has a

closeActivity
method. The interface looks like this:

public class JavaScriptInterface {

private Activity activity;

public JavaScriptInterface(Activity activity) {
this.activity = activity;
}

/**
* Starts a new activity
* @param page
* @return
*/
@JavascriptInterface
public boolean openActivity(String page){
if(isConnected()) {
Intent intent = new Intent(activity, GenericActivity.class);
intent.putExtra("page", page);
activity.startActivity(intent);
return true;
}
return false;
}

/**
* Closes the current activity
*/
@JavascriptInterface
public void closeActivity(){
activity.finish();
}
}


Here is the activity that gets launched:

public class GenericActivity extends AppCompatActivity {

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

Intent intent = getIntent();
String page = intent.getStringExtra("page");

XWalkView xWalkWebView = (XWalkView)findViewById(R.id.xwalkWebView);

// Turn on debugging if we are in DEBUG mode
if (BuildConfig.DEBUG) {
XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
xWalkWebView.clearCache(true);
}

// Add the connector to talk to/from javascript
JavaScriptInterface i = new JavaScriptInterface(this);
xWalkWebView.addJavascriptInterface(i, "jsConnector");

// Load the base file
xWalkWebView.loadAppFromManifest("file:///android_asset/manifest.json", null);
xWalkWebView.loadUrl("file:///android_asset/" + page + ".html", null);
}
}


When I call
closeActivity
the activity visually goes away, but in the chrome inspector (chrome://inspect), I can still see the crosswalk
webview
. Why is it still there and how can I close it?

Answer

Wow! this was a super easy fix.

In the activity, all I needed was to add view.onDestroy() like this:

@Override
public void onDestroy(){
    XWalkView view = (XWalkView)findViewById(R.id.xwalkWebView);
    view.onDestroy();
    super.onDestroy();
    finish();
}