Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I like your original idea of creating a "returnFromNotify" activity better than your proposed workaround, as it <em>is</em> possible to detect if the ResumeActivity is at the bottom of the stack (and therefore the only activity in the stack).</p> <p>Here's how you can do it:</p> <p>Add your ResumeActivity to the manifest and specify the <a href="http://developer.android.com/guide/topics/manifest/activity-element.html#nohist">noHistory</a> attribute:</p> <pre><code>&lt;activity android:name=".ResumeActivity" android:noHistory="true" /&gt; </code></pre> <p>Specifying noHistory will make sure this Activity won't stay in the stack as soon as it finishes. This way you know that only a currently running instance of the ResumeActivity will show up in the stack.</p> <p>In order to check the application stack, you'll also have to ask for the GET_TASKS permission:</p> <pre><code>&lt;uses-permission android:name="android.permission.GET_TASKS" /&gt; </code></pre> <p>Now you can use <a href="http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks%28int%29">ActivityManager::getRunningTasks()</a> to determine if ResumeActivity is the only activity in the stack:</p> <pre><code>public class ResumeActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(isOnlyActivityInStack()) { //check the application stack //This activity is the only activity in the application stack, so we need to launch the main activity Intent main = new Intent(this, MainActivity.class); main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(main); } else { //Return the user to the last activity they had open this.finish(); } } /** * Checks the currently running tasks. If this activity is the base activity, we know it's the only activity in the stack * * @return boolean This activity is the only activity in the stack? **/ private boolean isOnlyActivityInStack() { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); boolean onlyActivityInStack = false; for(RunningTaskInfo tasks : manager.getRunningTasks(Integer.MAX_VALUE)) { if(tasks.baseActivity.getPackageName().equals(this.getPackageName())) { //find this package's application stack if(tasks.baseActivity.getClassName().equals(this.getClass().getName())) { //If the ResumeActivity is the base activity, we know that it is the only activity in the stack onlyActivityInStack = true; break; } } } return onlyActivityInStack; } } </code></pre> <p>I know you asked this question over 2 years ago, but I'm providing this answer in case anyone else runs in to this particular situation (as I did). I think you were on the right track with the solution you were originally working towards.</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