Handle activity back stack



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have 4 activities:



  • MainActivity (launcher) - checks if user is logged in then start ContainerActivity else IntroActivity

  • IntroActivity - shows various login methods including facebook login

  • LoginActivity - contains phone or email login method. After successful login start ContainerActivity

  • ContainerActivity - main application

I have 2 problems.



First, I want to remove IntroActivity from stack when user successfully login from LoginActivity. Can't use android:noHistory="true" because when user decides to use facebook to login not phone or email, IntroActivity should be in stack.



Second, when user logout from any activity, I want to back to MainActivity and clearing all back stack. I will add activities in future. What is the right way to do this?



 <activity
android:name=".MainActivity"
android:theme="@style/AppTheme.Launcher"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".intro.IntroActivity">

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity
android:name=".authentication.LoginActivity"
android:parentActivityName=".intro.IntroActivity"
android:noHistory="true" />









share|improve this question
























  • This is not a Firebase related issue. Please don't add tags that do not correspond with your problem.

    – Alex Mamo
    Nov 15 '18 at 12:15

















1















I have 4 activities:



  • MainActivity (launcher) - checks if user is logged in then start ContainerActivity else IntroActivity

  • IntroActivity - shows various login methods including facebook login

  • LoginActivity - contains phone or email login method. After successful login start ContainerActivity

  • ContainerActivity - main application

I have 2 problems.



First, I want to remove IntroActivity from stack when user successfully login from LoginActivity. Can't use android:noHistory="true" because when user decides to use facebook to login not phone or email, IntroActivity should be in stack.



Second, when user logout from any activity, I want to back to MainActivity and clearing all back stack. I will add activities in future. What is the right way to do this?



 <activity
android:name=".MainActivity"
android:theme="@style/AppTheme.Launcher"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".intro.IntroActivity">

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity
android:name=".authentication.LoginActivity"
android:parentActivityName=".intro.IntroActivity"
android:noHistory="true" />









share|improve this question
























  • This is not a Firebase related issue. Please don't add tags that do not correspond with your problem.

    – Alex Mamo
    Nov 15 '18 at 12:15













1












1








1








I have 4 activities:



  • MainActivity (launcher) - checks if user is logged in then start ContainerActivity else IntroActivity

  • IntroActivity - shows various login methods including facebook login

  • LoginActivity - contains phone or email login method. After successful login start ContainerActivity

  • ContainerActivity - main application

I have 2 problems.



First, I want to remove IntroActivity from stack when user successfully login from LoginActivity. Can't use android:noHistory="true" because when user decides to use facebook to login not phone or email, IntroActivity should be in stack.



Second, when user logout from any activity, I want to back to MainActivity and clearing all back stack. I will add activities in future. What is the right way to do this?



 <activity
android:name=".MainActivity"
android:theme="@style/AppTheme.Launcher"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".intro.IntroActivity">

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity
android:name=".authentication.LoginActivity"
android:parentActivityName=".intro.IntroActivity"
android:noHistory="true" />









share|improve this question
















I have 4 activities:



  • MainActivity (launcher) - checks if user is logged in then start ContainerActivity else IntroActivity

  • IntroActivity - shows various login methods including facebook login

  • LoginActivity - contains phone or email login method. After successful login start ContainerActivity

  • ContainerActivity - main application

I have 2 problems.



First, I want to remove IntroActivity from stack when user successfully login from LoginActivity. Can't use android:noHistory="true" because when user decides to use facebook to login not phone or email, IntroActivity should be in stack.



Second, when user logout from any activity, I want to back to MainActivity and clearing all back stack. I will add activities in future. What is the right way to do this?



 <activity
android:name=".MainActivity"
android:theme="@style/AppTheme.Launcher"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".intro.IntroActivity">

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity
android:name=".authentication.LoginActivity"
android:parentActivityName=".intro.IntroActivity"
android:noHistory="true" />






android android-activity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:16









Alex Mamo

47.4k82965




47.4k82965










asked Nov 15 '18 at 10:30









Мөнхбаатар ГанбаатарМөнхбаатар Ганбаатар

135




135












  • This is not a Firebase related issue. Please don't add tags that do not correspond with your problem.

    – Alex Mamo
    Nov 15 '18 at 12:15

















  • This is not a Firebase related issue. Please don't add tags that do not correspond with your problem.

    – Alex Mamo
    Nov 15 '18 at 12:15
















This is not a Firebase related issue. Please don't add tags that do not correspond with your problem.

– Alex Mamo
Nov 15 '18 at 12:15





This is not a Firebase related issue. Please don't add tags that do not correspond with your problem.

– Alex Mamo
Nov 15 '18 at 12:15












2 Answers
2






active

oldest

votes


















0














To remove IntroActivity from stack after successful login, I would do this:



Assuming that LoginActivity should also be finished (removed from the stack) on successful login, you can call startActivity() to return to IntroActivity and add an "extra" to the Intent telling IntroActivity to start ContainerActivity and finish itself. Do this:



in LoginActivity:



Intent intent = new Intent(this, IntroActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("startContainerActivity", true);
startActivity(intent);
finish();


in IntroActivity.onNewIntent():



if (intent.hasExtra("startContainerActivity")) 
Intent launchIntent = new Intent(this, ContainerActivity.class);
startActivity(launchIntent);
finish();



IntroActivity will then launch ContainerActivity and finish itself, leaving the stack: MainActivity->ContainerActivity.



To return to MainActivity from any other Activity, you just need to override onBackPressed() and return to MainActivity.



@Override
public void onBackPressed() Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();



When you specify the flags CLEAR_TOP and SINGLE_TOP this tells Android to remove all the activities on top of the target Activity and to reuse the target Activity (ie: not create a new instance of the target Activity). In this case, onNewIntent() is called on the existing instance of the Activity.






share|improve this answer






























    0














    You can start IntoActivity using startActivityForResult and in LoginActivity after successfully loged in, you can set result RESULT_OK and in IntroActivity onActivityResult check for result code and finish IntoActivity.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317380%2fhandle-activity-back-stack%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      To remove IntroActivity from stack after successful login, I would do this:



      Assuming that LoginActivity should also be finished (removed from the stack) on successful login, you can call startActivity() to return to IntroActivity and add an "extra" to the Intent telling IntroActivity to start ContainerActivity and finish itself. Do this:



      in LoginActivity:



      Intent intent = new Intent(this, IntroActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
      intent.putExtra("startContainerActivity", true);
      startActivity(intent);
      finish();


      in IntroActivity.onNewIntent():



      if (intent.hasExtra("startContainerActivity")) 
      Intent launchIntent = new Intent(this, ContainerActivity.class);
      startActivity(launchIntent);
      finish();



      IntroActivity will then launch ContainerActivity and finish itself, leaving the stack: MainActivity->ContainerActivity.



      To return to MainActivity from any other Activity, you just need to override onBackPressed() and return to MainActivity.



      @Override
      public void onBackPressed() Intent.FLAG_ACTIVITY_SINGLE_TOP);
      startActivity(intent);
      finish();



      When you specify the flags CLEAR_TOP and SINGLE_TOP this tells Android to remove all the activities on top of the target Activity and to reuse the target Activity (ie: not create a new instance of the target Activity). In this case, onNewIntent() is called on the existing instance of the Activity.






      share|improve this answer



























        0














        To remove IntroActivity from stack after successful login, I would do this:



        Assuming that LoginActivity should also be finished (removed from the stack) on successful login, you can call startActivity() to return to IntroActivity and add an "extra" to the Intent telling IntroActivity to start ContainerActivity and finish itself. Do this:



        in LoginActivity:



        Intent intent = new Intent(this, IntroActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("startContainerActivity", true);
        startActivity(intent);
        finish();


        in IntroActivity.onNewIntent():



        if (intent.hasExtra("startContainerActivity")) 
        Intent launchIntent = new Intent(this, ContainerActivity.class);
        startActivity(launchIntent);
        finish();



        IntroActivity will then launch ContainerActivity and finish itself, leaving the stack: MainActivity->ContainerActivity.



        To return to MainActivity from any other Activity, you just need to override onBackPressed() and return to MainActivity.



        @Override
        public void onBackPressed() Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();



        When you specify the flags CLEAR_TOP and SINGLE_TOP this tells Android to remove all the activities on top of the target Activity and to reuse the target Activity (ie: not create a new instance of the target Activity). In this case, onNewIntent() is called on the existing instance of the Activity.






        share|improve this answer

























          0












          0








          0







          To remove IntroActivity from stack after successful login, I would do this:



          Assuming that LoginActivity should also be finished (removed from the stack) on successful login, you can call startActivity() to return to IntroActivity and add an "extra" to the Intent telling IntroActivity to start ContainerActivity and finish itself. Do this:



          in LoginActivity:



          Intent intent = new Intent(this, IntroActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          intent.putExtra("startContainerActivity", true);
          startActivity(intent);
          finish();


          in IntroActivity.onNewIntent():



          if (intent.hasExtra("startContainerActivity")) 
          Intent launchIntent = new Intent(this, ContainerActivity.class);
          startActivity(launchIntent);
          finish();



          IntroActivity will then launch ContainerActivity and finish itself, leaving the stack: MainActivity->ContainerActivity.



          To return to MainActivity from any other Activity, you just need to override onBackPressed() and return to MainActivity.



          @Override
          public void onBackPressed() Intent.FLAG_ACTIVITY_SINGLE_TOP);
          startActivity(intent);
          finish();



          When you specify the flags CLEAR_TOP and SINGLE_TOP this tells Android to remove all the activities on top of the target Activity and to reuse the target Activity (ie: not create a new instance of the target Activity). In this case, onNewIntent() is called on the existing instance of the Activity.






          share|improve this answer













          To remove IntroActivity from stack after successful login, I would do this:



          Assuming that LoginActivity should also be finished (removed from the stack) on successful login, you can call startActivity() to return to IntroActivity and add an "extra" to the Intent telling IntroActivity to start ContainerActivity and finish itself. Do this:



          in LoginActivity:



          Intent intent = new Intent(this, IntroActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          intent.putExtra("startContainerActivity", true);
          startActivity(intent);
          finish();


          in IntroActivity.onNewIntent():



          if (intent.hasExtra("startContainerActivity")) 
          Intent launchIntent = new Intent(this, ContainerActivity.class);
          startActivity(launchIntent);
          finish();



          IntroActivity will then launch ContainerActivity and finish itself, leaving the stack: MainActivity->ContainerActivity.



          To return to MainActivity from any other Activity, you just need to override onBackPressed() and return to MainActivity.



          @Override
          public void onBackPressed() Intent.FLAG_ACTIVITY_SINGLE_TOP);
          startActivity(intent);
          finish();



          When you specify the flags CLEAR_TOP and SINGLE_TOP this tells Android to remove all the activities on top of the target Activity and to reuse the target Activity (ie: not create a new instance of the target Activity). In this case, onNewIntent() is called on the existing instance of the Activity.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 20:03









          David WasserDavid Wasser

          70.6k10144204




          70.6k10144204























              0














              You can start IntoActivity using startActivityForResult and in LoginActivity after successfully loged in, you can set result RESULT_OK and in IntroActivity onActivityResult check for result code and finish IntoActivity.






              share|improve this answer



























                0














                You can start IntoActivity using startActivityForResult and in LoginActivity after successfully loged in, you can set result RESULT_OK and in IntroActivity onActivityResult check for result code and finish IntoActivity.






                share|improve this answer

























                  0












                  0








                  0







                  You can start IntoActivity using startActivityForResult and in LoginActivity after successfully loged in, you can set result RESULT_OK and in IntroActivity onActivityResult check for result code and finish IntoActivity.






                  share|improve this answer













                  You can start IntoActivity using startActivityForResult and in LoginActivity after successfully loged in, you can set result RESULT_OK and in IntroActivity onActivityResult check for result code and finish IntoActivity.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 11:50









                  SANDIP CHAUDHARISANDIP CHAUDHARI

                  8115




                  8115



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317380%2fhandle-activity-back-stack%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo