Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a really nice trick. My problem was that some really old C++ jni library leaked resources. At some point, it stopped functioning. The user tried to exit the app and launch it again -- with no result, because finishing an activity is not the same as finishing (or killing) the process. (By the way, the user could go to the list of the running applications and stop it from there -- this would work, but the users just do not know how to terminate applications.) </p> <p>If you want to observe the effect of this feature, add a <code>static</code> variable to your activity and increment it each, say, button press. If you exit the application activity and then invoke the application again, this static variable will keep its value. (If the application really was exited, the variable would be assigned the initial value.)</p> <p>(And I have to comment on why I did not want to <em>fix</em> the bug instead. The library was written decades ago and leaked resources ever since. The management believes it always <em>worked</em>. The <em>cost</em> of providing a fix instead of a workaround... I think, you get the idea.)</p> <p>Now, how could I reset a jni shared (a.k.a. dynamic, .so) library to the initial state? I chose to restart application as a new process.</p> <p>The trick is that System.exit() closes the current activity and Android recreates the application with one activity less.</p> <p>So the code is:</p> <pre><code>/** This activity shows nothing; instead, it restarts the android process */ public class MagicAppRestart extends Activity { // Do not forget to add it to AndroidManifest.xml // &lt;activity android:name="your.package.name.MagicAppRestart"/&gt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.exit(0); } public static void doRestart(Activity anyActivity) { anyActivity.startActivity(new Intent(anyActivity.getApplicationContext(), MagicAppRestart.class)); } } </code></pre> <p>The calling activity just executes the code <code>MagicAppRestart.doRestart(this);</code>, the calling activity's <code>onPause()</code> is executed, and then the process is re-created. And do not forget to mention this activity in AndroidManifest.xml</p> <p>The advantage of this method is that there is no delays.</p> <p>UPD: it worked in Android 2.x, but in Android 4 something has changed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload