Clear upto a specific activity from the stack history












0














Suppose I have activities named A, B, C, D. Now, consider A has been launched as the root activity and B has been launched from A and C has been launched from B and D has been launched from C.
Now I have a button named "Success" in the activity D. If suppose, I press the button "Success" in the activity D, then the activity C should be removed from the history stack and go to activity B, but on Pressing back key from the activity D should display activity C instead of B and Clear D from stack. Please can anyone help me to resolve this problem?










share|improve this question






















  • If you organised this in activities, and are all part of a, let's say, step by step which my vary depending on the previous, I strongly believe in the power of startActivityForResult and onActivityResult
    – Korcholis
    Nov 22 at 12:39
















0














Suppose I have activities named A, B, C, D. Now, consider A has been launched as the root activity and B has been launched from A and C has been launched from B and D has been launched from C.
Now I have a button named "Success" in the activity D. If suppose, I press the button "Success" in the activity D, then the activity C should be removed from the history stack and go to activity B, but on Pressing back key from the activity D should display activity C instead of B and Clear D from stack. Please can anyone help me to resolve this problem?










share|improve this question






















  • If you organised this in activities, and are all part of a, let's say, step by step which my vary depending on the previous, I strongly believe in the power of startActivityForResult and onActivityResult
    – Korcholis
    Nov 22 at 12:39














0












0








0







Suppose I have activities named A, B, C, D. Now, consider A has been launched as the root activity and B has been launched from A and C has been launched from B and D has been launched from C.
Now I have a button named "Success" in the activity D. If suppose, I press the button "Success" in the activity D, then the activity C should be removed from the history stack and go to activity B, but on Pressing back key from the activity D should display activity C instead of B and Clear D from stack. Please can anyone help me to resolve this problem?










share|improve this question













Suppose I have activities named A, B, C, D. Now, consider A has been launched as the root activity and B has been launched from A and C has been launched from B and D has been launched from C.
Now I have a button named "Success" in the activity D. If suppose, I press the button "Success" in the activity D, then the activity C should be removed from the history stack and go to activity B, but on Pressing back key from the activity D should display activity C instead of B and Clear D from stack. Please can anyone help me to resolve this problem?







android






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 12:29









Nick

12




12












  • If you organised this in activities, and are all part of a, let's say, step by step which my vary depending on the previous, I strongly believe in the power of startActivityForResult and onActivityResult
    – Korcholis
    Nov 22 at 12:39


















  • If you organised this in activities, and are all part of a, let's say, step by step which my vary depending on the previous, I strongly believe in the power of startActivityForResult and onActivityResult
    – Korcholis
    Nov 22 at 12:39
















If you organised this in activities, and are all part of a, let's say, step by step which my vary depending on the previous, I strongly believe in the power of startActivityForResult and onActivityResult
– Korcholis
Nov 22 at 12:39




If you organised this in activities, and are all part of a, let's say, step by step which my vary depending on the previous, I strongly believe in the power of startActivityForResult and onActivityResult
– Korcholis
Nov 22 at 12:39












2 Answers
2






active

oldest

votes


















0














Add this Code in your Activity_D for Button click



    btnSuccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_D.this, Activity_B.class));
finishAffinity();
}
});


Also add this in your Activity_D to Handle Back Button Pressed.



@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}


From Activity_C to Activity_D



startActivity(new Intent(Activity_C.this, Activity_D.class));


Don't call finish() here.






share|improve this answer





















  • Thanks. But as per your code, the stack is not clearing. and when I click back button from B it will go to activity D. So I hope this will not work for me.
    – Nick
    Nov 22 at 12:36












  • Have you added finishAffinity()?
    – Ali Ahmed
    Nov 22 at 12:39



















0














When you click on Success button, you can use the following line of code to open your B activity:



Intent d_intent = new Intent(D.this, B.class);
d_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(d_intent);
finish();


Here Activity B will be started from the back stack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one. So when we press the back button the whole application will be terminated. And on Back press you need to simply call



finish();


Hope it will help you out.






share|improve this answer























  • Thanks, I tried this. But I need to be like when I click back button from B I need to go to Activity A, Application shouldn't get close.
    – Nick
    Nov 22 at 12:56










  • In that case, you can try this without Intent.FLAG_ACTIVITY_NEW_TASK flag
    – Rahul Sharma
    Nov 22 at 12:59













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%2f53431062%2fclear-upto-a-specific-activity-from-the-stack-history%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














Add this Code in your Activity_D for Button click



    btnSuccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_D.this, Activity_B.class));
finishAffinity();
}
});


Also add this in your Activity_D to Handle Back Button Pressed.



@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}


From Activity_C to Activity_D



startActivity(new Intent(Activity_C.this, Activity_D.class));


Don't call finish() here.






share|improve this answer





















  • Thanks. But as per your code, the stack is not clearing. and when I click back button from B it will go to activity D. So I hope this will not work for me.
    – Nick
    Nov 22 at 12:36












  • Have you added finishAffinity()?
    – Ali Ahmed
    Nov 22 at 12:39
















0














Add this Code in your Activity_D for Button click



    btnSuccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_D.this, Activity_B.class));
finishAffinity();
}
});


Also add this in your Activity_D to Handle Back Button Pressed.



@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}


From Activity_C to Activity_D



startActivity(new Intent(Activity_C.this, Activity_D.class));


Don't call finish() here.






share|improve this answer





















  • Thanks. But as per your code, the stack is not clearing. and when I click back button from B it will go to activity D. So I hope this will not work for me.
    – Nick
    Nov 22 at 12:36












  • Have you added finishAffinity()?
    – Ali Ahmed
    Nov 22 at 12:39














0












0








0






Add this Code in your Activity_D for Button click



    btnSuccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_D.this, Activity_B.class));
finishAffinity();
}
});


Also add this in your Activity_D to Handle Back Button Pressed.



@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}


From Activity_C to Activity_D



startActivity(new Intent(Activity_C.this, Activity_D.class));


Don't call finish() here.






share|improve this answer












Add this Code in your Activity_D for Button click



    btnSuccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_D.this, Activity_B.class));
finishAffinity();
}
});


Also add this in your Activity_D to Handle Back Button Pressed.



@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}


From Activity_C to Activity_D



startActivity(new Intent(Activity_C.this, Activity_D.class));


Don't call finish() here.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 12:32









Ali Ahmed

1,2751314




1,2751314












  • Thanks. But as per your code, the stack is not clearing. and when I click back button from B it will go to activity D. So I hope this will not work for me.
    – Nick
    Nov 22 at 12:36












  • Have you added finishAffinity()?
    – Ali Ahmed
    Nov 22 at 12:39


















  • Thanks. But as per your code, the stack is not clearing. and when I click back button from B it will go to activity D. So I hope this will not work for me.
    – Nick
    Nov 22 at 12:36












  • Have you added finishAffinity()?
    – Ali Ahmed
    Nov 22 at 12:39
















Thanks. But as per your code, the stack is not clearing. and when I click back button from B it will go to activity D. So I hope this will not work for me.
– Nick
Nov 22 at 12:36






Thanks. But as per your code, the stack is not clearing. and when I click back button from B it will go to activity D. So I hope this will not work for me.
– Nick
Nov 22 at 12:36














Have you added finishAffinity()?
– Ali Ahmed
Nov 22 at 12:39




Have you added finishAffinity()?
– Ali Ahmed
Nov 22 at 12:39













0














When you click on Success button, you can use the following line of code to open your B activity:



Intent d_intent = new Intent(D.this, B.class);
d_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(d_intent);
finish();


Here Activity B will be started from the back stack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one. So when we press the back button the whole application will be terminated. And on Back press you need to simply call



finish();


Hope it will help you out.






share|improve this answer























  • Thanks, I tried this. But I need to be like when I click back button from B I need to go to Activity A, Application shouldn't get close.
    – Nick
    Nov 22 at 12:56










  • In that case, you can try this without Intent.FLAG_ACTIVITY_NEW_TASK flag
    – Rahul Sharma
    Nov 22 at 12:59


















0














When you click on Success button, you can use the following line of code to open your B activity:



Intent d_intent = new Intent(D.this, B.class);
d_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(d_intent);
finish();


Here Activity B will be started from the back stack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one. So when we press the back button the whole application will be terminated. And on Back press you need to simply call



finish();


Hope it will help you out.






share|improve this answer























  • Thanks, I tried this. But I need to be like when I click back button from B I need to go to Activity A, Application shouldn't get close.
    – Nick
    Nov 22 at 12:56










  • In that case, you can try this without Intent.FLAG_ACTIVITY_NEW_TASK flag
    – Rahul Sharma
    Nov 22 at 12:59
















0












0








0






When you click on Success button, you can use the following line of code to open your B activity:



Intent d_intent = new Intent(D.this, B.class);
d_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(d_intent);
finish();


Here Activity B will be started from the back stack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one. So when we press the back button the whole application will be terminated. And on Back press you need to simply call



finish();


Hope it will help you out.






share|improve this answer














When you click on Success button, you can use the following line of code to open your B activity:



Intent d_intent = new Intent(D.this, B.class);
d_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(d_intent);
finish();


Here Activity B will be started from the back stack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one. So when we press the back button the whole application will be terminated. And on Back press you need to simply call



finish();


Hope it will help you out.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 13:05









Rahul Sdei

31




31










answered Nov 22 at 12:39









Rahul Sharma

2,1391418




2,1391418












  • Thanks, I tried this. But I need to be like when I click back button from B I need to go to Activity A, Application shouldn't get close.
    – Nick
    Nov 22 at 12:56










  • In that case, you can try this without Intent.FLAG_ACTIVITY_NEW_TASK flag
    – Rahul Sharma
    Nov 22 at 12:59




















  • Thanks, I tried this. But I need to be like when I click back button from B I need to go to Activity A, Application shouldn't get close.
    – Nick
    Nov 22 at 12:56










  • In that case, you can try this without Intent.FLAG_ACTIVITY_NEW_TASK flag
    – Rahul Sharma
    Nov 22 at 12:59


















Thanks, I tried this. But I need to be like when I click back button from B I need to go to Activity A, Application shouldn't get close.
– Nick
Nov 22 at 12:56




Thanks, I tried this. But I need to be like when I click back button from B I need to go to Activity A, Application shouldn't get close.
– Nick
Nov 22 at 12:56












In that case, you can try this without Intent.FLAG_ACTIVITY_NEW_TASK flag
– Rahul Sharma
Nov 22 at 12:59






In that case, you can try this without Intent.FLAG_ACTIVITY_NEW_TASK flag
– Rahul Sharma
Nov 22 at 12:59




















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53431062%2fclear-upto-a-specific-activity-from-the-stack-history%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

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...