Skip to content

Commit a4a3f8a

Browse files
committed
kill python thread on exit and handle back button
1 parent 90658f7 commit a4a3f8a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pythonforandroid/bootstraps/webview/build/src/org/kivy/android/PythonActivity.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,15 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
180180

181181
final Thread wvThread = new Thread(new WebViewLoaderMain(), "WvThread");
182182
wvThread.start();
183+
}
183184

185+
@Override
186+
public void onDestroy() {
187+
Log.i("Destroy", "end of app");
188+
super.onDestroy();
189+
190+
// make sure all child threads (python_thread) are stopped
191+
android.os.Process.killProcess(android.os.Process.myPid());
184192
}
185193

186194
public void loadLibraries() {
@@ -297,6 +305,27 @@ public static ViewGroup getLayout() {
297305
return mLayout;
298306
}
299307

308+
long lastBackClick = SystemClock.elapsedRealtime();
309+
@Override
310+
public boolean onKeyDown(int keyCode, KeyEvent event) {
311+
// Check if the key event was the Back button and if there's history
312+
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
313+
mWebView.goBack();
314+
return true;
315+
}
316+
// If it wasn't the Back key or there's no web page history, bubble up to the default
317+
// system behavior (probably exit the activity)
318+
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
319+
lastBackClick = SystemClock.elapsedRealtime();
320+
Toast.makeText(this, "Click again to close the app",
321+
Toast.LENGTH_LONG).show();
322+
return true;
323+
}
324+
325+
lastBackClick = SystemClock.elapsedRealtime();
326+
return super.onKeyDown(keyCode, event);
327+
}
328+
300329

301330
//----------------------------------------------------------------------------
302331
// Listener interface for onNewIntent
@@ -367,7 +396,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
367396
}
368397
}
369398

370-
public static void start_service(String serviceTitle, String serviceDescription,
399+
public static void start_service(String serviceTitle, String serviceDescription,
371400
String pythonServiceArgument) {
372401
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
373402
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();

pythonforandroid/toolchain.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def build_dist_from_args(ctx, dist, args):
118118
ctx.recipe_build_order = build_order
119119
ctx.python_modules = python_modules
120120

121+
if python_modules and hasattr(sys, 'real_prefix'):
122+
error('virtualenv is needed to install pure-Python modules, but')
123+
error('virtualenv does not support nesting, and you are running')
124+
error('python-for-android in one. Please run p4a outside of a')
125+
error('virtualenv instead.')
126+
exit(1)
127+
121128
info('The selected bootstrap is {}'.format(bs.name))
122129
info_main('# Creating dist with {} bootstrap'.format(bs.name))
123130
bs.distribution = dist

0 commit comments

Comments
 (0)