Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>class MagicAppRestart</code> from <a href="https://stackoverflow.com/a/13173279/755804">this post</a> to <strong><em>kill the process</em></strong> along with all AsyncTasks; Android will restore the activity stack (the user will not mention anything). It is important to note that the only notification before a process restart is calling <code>onPause()</code>; according to the <a href="http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle" rel="nofollow noreferrer">Android app lifecycle logic</a>, your application must be ready to such termination anyway.</p> <p>I have tried it, and it seems to work. Nevertheless, at the the moment I plan to use "more civilized" methods like weak references from the Application class (my AsyncTasks are rather short-living and hopefully not so much memory-consuming).</p> <p>Here is some code you can play with:</p> <p><em>MagicAppRestart.java</em></p> <pre><code>package com.xyz; import android.app.Activity; import android.content.Intent; import android.os.Bundle; /** 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 rest is what Eclipse created for a new Android project for <em>com.xyz.AsyncTaskTestActivity</em>:</p> <p><em>AsyncTaskTestActivity.java</em></p> <pre><code>package com.xyz; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; public class AsyncTaskTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Log.d("~~~~","~~~onCreate ~~~ "+this); super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onStartButton(View view) { Log.d("~~~~","~~~onStartButton {"); class MyTask extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub Log.d("~~~~","~~~doInBackground started"); try { for (int i=0; i&lt;10; i++) { Log.d("~~~~","~~~sleep#"+i); Thread.sleep(200); } Log.d("~~~~","~~~sleeping over"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.d("~~~~","~~~doInBackground ended"); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); taskDone(); } } MyTask task = new MyTask(); task.execute(null); Log.d("~~~~","~~~onStartButton }"); } private void taskDone() { Log.d("~~~~","\n\n~~~taskDone ~~~ "+this+"\n\n"); } public void onStopButton(View view) { Log.d("~~~~","~~~onStopButton {"); MagicAppRestart.doRestart(this); Log.d("~~~~","~~~onStopButton }"); } public void onPause() { Log.d("~~~~","~~~onPause ~~~ "+this); super.onPause(); } public void onStop() { Log.d("~~~~","~~~onStop ~~~ "+this); super.onPause(); } public void onDestroy() { Log.d("~~~~","~~~onDestroy ~~~ "+this); super.onDestroy(); } } </code></pre> <p><em>main.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;Button android:text="Start" android:onClick="onStartButton" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;Button android:text="Stop" android:onClick="onStopButton" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><em>AndroidManifest.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xyz" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="7" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name=".AsyncTaskTestActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".MagicAppRestart"/&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>and a relevant part of the logs (note that <strong><em>only <code>onPause</code> is called</em></strong>):</p> <pre><code>D/~~~~ (13667): ~~~onStartButton { D/~~~~ (13667): ~~~onStartButton } D/~~~~ (13667): ~~~doInBackground started D/~~~~ (13667): ~~~sleep#0 D/~~~~ (13667): ~~~sleep#1 D/~~~~ (13667): ~~~sleep#2 D/~~~~ (13667): ~~~sleep#3 D/~~~~ (13667): ~~~sleep#4 D/~~~~ (13667): ~~~sleep#5 D/~~~~ (13667): ~~~sleep#6 D/~~~~ (13667): ~~~sleep#7 D/~~~~ (13667): ~~~sleep#8 D/~~~~ (13667): ~~~sleep#9 D/~~~~ (13667): ~~~sleeping over D/~~~~ (13667): ~~~doInBackground ended D/~~~~ (13667): D/~~~~ (13667): D/~~~~ (13667): ~~~taskDone ~~~ com.xyz.AsyncTaskTestActivity@40516988 D/~~~~ (13667): D/~~~~ (13667): ~~~onStartButton { D/~~~~ (13667): ~~~onStartButton } D/~~~~ (13667): ~~~doInBackground started D/~~~~ (13667): ~~~sleep#0 D/~~~~ (13667): ~~~sleep#1 D/~~~~ (13667): ~~~sleep#2 D/~~~~ (13667): ~~~sleep#3 D/~~~~ (13667): ~~~sleep#4 D/~~~~ (13667): ~~~sleep#5 D/~~~~ (13667): ~~~onStopButton { I/ActivityManager( 81): Starting: Intent { cmp=com.xyz/.MagicAppRestart } from pid 13667 D/~~~~ (13667): ~~~onStopButton } D/~~~~ (13667): ~~~onPause ~~~ com.xyz.AsyncTaskTestActivity@40516988 I/ActivityManager( 81): Process com.xyz (pid 13667) has died. I/WindowManager( 81): WIN DEATH: Window{4073ceb8 com.xyz/com.xyz.AsyncTaskTestActivity paused=false} I/ActivityManager( 81): Start proc com.xyz for activity com.xyz/.AsyncTaskTestActivity: pid=13698 uid=10101 gids={} I/ActivityManager( 81): Displayed com.xyz/.AsyncTaskTestActivity: +44ms (total +65ms) D/~~~~ (13698): ~~~onCreate ~~~ com.xyz.AsyncTaskTestActivity@40517238 </code></pre>
    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.
    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