Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It seems a similar question has already been asked. It sounds like the OP came up with a working solution. <a href="https://stackoverflow.com/questions/9894522/how-do-i-collapse-child-activities">How do I collapse &quot;child activities&quot;?</a></p> <p>EDIT: Instead of using a button you can use a boolean to tell whether or not you need to collapse back to the main activity. Have your root activity extend from Activity and the child activities extend from CollapsableActivity. To get this to work in all cases I added startOutsideActivity() and startOutsideActivityForResult().</p> <pre><code>import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class CollapsableActivity extends Activity { private boolean returnToRoot; public static final int COLLAPSE_BACK = -1; // something other than RESULT_CANEL (0) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); returnToRoot = true; } @Override protected void onStart() { super.onStart(); returnToRoot = true; } @Override protected void onRestart() { super.onRestart(); // start collapsing the stack if (returnToRoot) { setResult(COLLAPSE_BACK); finish(); } } @Override public void startActivityForResult(Intent intent, int requestCode) { super.startActivityForResult(intent, requestCode); returnToRoot = false; } public void startOutsideActivityForResult(Intent intent, int requestCode) { super.startActivityForResult(intent, requestCode); returnToRoot = true; } @Override public void startActivity(Intent intent) { // call startActivityForResult to make sure and catch the collapse condition super.startActivityForResult(intent, 0); returnToRoot = false; } public void startOutsideActivity(Intent intent) { super.startActivity(intent); returnToRoot = true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == COLLAPSE_BACK) { returnToRoot = true; } } } </code></pre> <p>This worked properly for me in all cases you listed. The only difference is you need to call startOutsideActivity() or startOutsideActivityForResult() when you navigate away from you app. Personally, I think this adds clarity to your intentions. Hope it helps!</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