Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid : Stop Activity(s) after starting Service
    text
    copied!<p>I have the following flow in my code:</p> <pre><code> Activity1 : startActivity(Activity2) ---&gt; Activity2: startActivity(Activity3) ---&gt; Activity3: startService(MyService) ---&gt; MyService: startActivity(Activity4) </code></pre> <p>Each Activity above shows a single view and represents a step in a 4-step setup. The final Activity - <code>Activity 4</code> - is started after some setup work is done inside <code>MyService</code>, which basically tells the user, </p> <p><code>"The service has started, you can close the application by pressing Back or Home button"</code></p> <p>When the user presses Back or Home, I want to destroy <code>Activities 1-4</code> , and only have <code>MyService</code> running. Also, after stopping the Application as above, when the user navigates back to the Application via the menu and starts it, I'll be checking if <code>MyService</code> is already running. If it is already running, I don't want to show <code>Activities 1-3</code>, I want to show another <code>Control Panel View</code> (Another Activity), which says,</p> <p><code>"Dude, the service is already running, do you want to Stop or Restart it?"</code></p> <p>This view will have a Stop and Restart button, to do the appropriate tasks.</p> <p><strong>My Questions:</strong></p> <ol> <li><p>How do I stop Activities 1-4 from inside Activity 4 when Back or Home is pressed,safely? My thought was to add a <code>static stopActivity()</code> method to each Activity, and calling <code>Activity[1-3].stopActivity()</code> from <code>onBackPressed()</code> or <code>onPause()</code> of <strong>Activity4</strong>. Then inside each <code>stopActivity()</code>, I'll call <code>finish()</code>, thus ending each Activity. But is it safe and efficient to do it this way?</p></li> <li><p>The flow I have illustrated above, is it the optimal way of doing things, or is there a cleaner way? I have <code>BroadcastReceivers</code> registered in these Activities, so I need to perform clean exits for each Activity, without leaked receivers, or worse, crashing the App or affect the User's phone due to unclean exit strategies.</p></li> </ol> <p>Thanks for your suggestions.</p>
 

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