Asked  7 Months ago    Answers:  5   Viewed   64 times

In Android I have some activities, let's say A, B, C.

In A, I use this code to open B:

Intent intent = new Intent(this, B.class);

In B, I use this code to open C:

Intent intent = new Intent(this, C.class);

When the user taps a button in C, I want to go back to A and clear the back stack (close both B and C). So when the user use the back button B and C will not show up, I've been trying the following:

Intent intent = new Intent(this, A.class);

But B and C are still showing up if I use the back button when I'm back in activity A. How can I avoid this?



Try adding FLAG_ACTIVITY_NEW_TASK as described in the docs for FLAG_ACTIVITY_CLEAR_TOP:

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

So your code to launch A would be:

Intent intent = new Intent(this, A.class);
CurrentActivity.this.finish(); // if the activity running has it's own context

// view.getContext().finish() for fragments etc.
Tuesday, June 1, 2021
answered 7 Months ago

Android activities are stored in the activity stack. Going back to a previous activity could mean two things.

  1. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

  2. Keep track of the activity stack. Whenever you start a new activity with an intent you can specify an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP. You can use this to shuffle between the activities in your application. Haven't used them much though. Have a look at the flags here:

As mentioned in the comments, if the activity is opened with startActivity() then one can close it with finish(). If you wish to use the Up button you can catch that in onOptionsSelected(MenuItem item) method with checking the item ID against unlike as mentioned in the comments.

Tuesday, June 1, 2021
answered 7 Months ago

I'll focus on answering how to resolve your workround so that it behaves as you want.

To capture actions performed on one Activity within another requires three steps.

Launch the secondary Activity (your 'camera Activity') as a subactivity by using startActivityForResult instead of startActivity.

Intent i = new Intent(this,CameraActivity.class);    
startActivityForResult(i, STATIC_INTEGER_VALUE);

Within the subactivity (camera Activity), rather than just closing the Activity when a user clicks the different tab image, you need to create a new Intent and include the index of the tab to display when you return to the parent app using the extras bundle. To pass it back to the parent call setResult before calling finish to close the camera Activity.

resultIntent = new Intent(null);
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, tabIndexValue);
setResult(Activity.RESULT_OK, resultIntent);

The final step is in the calling Activity, override onActivityResult to listen for callbacks from the camera Activity. Get the extra from the returned Intent to determine the index of the tab you should be displaying.

public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 
  switch(requestCode) { 
    case (STATIC_INTEGER_VALUE) : { 
      if (resultCode == Activity.RESULT_OK) { 
      int tabIndex = data.getIntExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
      // TODO Switch tabs using the index.
Wednesday, June 9, 2021
answered 6 Months ago

@SorryForMyEnglish's answer is right. You just cannot to implement it. By using android:noHistory="boolean" attribute, see my concept maps below:


Because of ActivityC and ActivityD (last activities) has a true value, so they cannot back to MainActivity, but they can back to ActivityA and ActivityB. Also, ActivityA and ActivityB can back to MainActivity. And the backstack is completely cleared without using this startActivity(intent) to open your next Activity (so you will need FLAG):

Intent intent = new Intent(CurrentActivity.this, NextActivityToBeOpened.class);

After you declared the value in manifest, you just need to call this startActivity(intent) to open the next Activity (no FLAG is needed):

startActivity(new Intent(CurrentActivity.this, NextActivityToBeOpened.class));

Is it very simple, right?


  • Set your last Activity with android:noHistory="true" in manifest.
  • Another Activity must be set with false value.
  • Apply this attribute for all of your Activity.

As additional, here is how to use it inside your manifest:

<activity android:name=".MyActivity" android:noHistory="true" />
Wednesday, July 7, 2021
answered 5 Months ago

I extended the clickableSpan class and passed it a flag that lets me know that I should highlight it.

SpannableStringBuilder tag;
.... tag.setSpan(new WordSpan(i, tokens[i], wordtohighlitedID) { 


import android.text.TextPaint;
import android.view.View;

public class WordSpan extends ClickableSpan 

    private int id;
    private TextPaint textpaint;
    public boolean shouldHilightWord = false;
    public WordSpan(int anID, String txt, int selected) {
        id =anID;
        // if the word selected is the same as the ID set the highlight flag
        if(selected == id)  {
            shouldHilightWord = true;



    public void updateDrawState(TextPaint ds) {
        textpaint = ds;
            textpaint.bgColor = Color.GRAY;         
            textpaint.setARGB(255, 255, 255, 255);

        //Remove default underline associated with spans


    public void changeSpanBgColor(View widget){
        shouldHilightWord = true;

    public void onClick(View widget) {

        // TODO Auto-generated method stub


     * This function sets the span to record the word number, as the span ID
     * @param spanID
    public void setSpanTextID(int spanID){
        id = spanID;

     * Return the wordId of this span
     * @return id
    public int getSpanTextID(){
        return id;
Monday, August 2, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :