Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8275837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:11:51+00:00 2026-06-08T08:11:51+00:00

I have three activities ActivityA, ActivityB, ActivityC. Suppose in ActivityA, there is some code

  • 0

I have three activities ActivityA, ActivityB, ActivityC.

Suppose in ActivityA, there is some code like…

if(someCondition()){
gotoActivityB();
}
else{
gotoActivityC();
}

Now, If user goes to ActivityB, ActivityA should not be finished.

If he goes to ActivityC, it should be finished.

Adding noHistory in manifest file doesn’t work.

Also, finish()in if condition doesn’t work, As there are many activities after ActivityC in which ActivityA should be in background.

I don’t want to call startActivity(context,ActivityA.class)in those activities onBackPressed() because, it will again execute code of onCreate() in ActivityA.

So, is there a way, where i can remove ActivityA from the stack when user presses back button in ActivityB?

may be something like this:?

ActivityB.this.finish();
ActivityA.finish(); //some code to finish ActivityA
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:11:52+00:00Added an answer on June 8, 2026 at 8:11 am

    Okay, here is one way you can accomplish your goal. You will need to pass around the Activity context to wherever you need it in order to call finish() on it. I used the Application class to do this. I only used two classes to do it for the sake of time, but it should work just fine for your purposes. Here is how I did it:

    This is the first class. It is the Activity that we want to close from another Activity.

    public class MainActivity extends Activity implements OnClickListener {
    private Button button;
    // application instance
    private MainApplication mainApplication;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mainApplication = (MainApplication) getApplicationContext();
        // set the Activity's context for later usage.  Doing this determines which
        // Activity can be closed from another Activity.
        mainApplication.setActivityContext(this);
    
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
    }
    
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            Intent i = new Intent(this, SecondActivity.class);
            startActivity(i);
            break;
        }       
    } 
    }
    

    This is the Second Activity. Exiting out of it will also cause finish() to be called on the first class:

    public class SecondActivity extends Activity {
    
    private Activity activityContext;
    private MainApplication mainApplication;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity_layout);
    
        mainApplication = (MainApplication) getApplicationContext();
        // get the Activity context you stored in the MainApplication class
        // so you can call finish on it.
        activityContext = mainApplication.getActivityContext();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // closes your defined Activity.  If you press the back button you will find
        // that you exit right out of the app as the other Activity gets popped off 
        // the stack.
        activityContext.finish();
    }
    }
    

    And the Application class:

    public class MainApplication extends Application {
    
    private Activity activityContext;
    
    public Activity getActivityContext() {
        return activityContext;
    }
    
    public void setActivityContext(Activity activityContext) {
        this.activityContext = activityContext;
    }
    
    }
    

    And of course make sure to declare your MainApplication class in the AndroidManifest:

    <application
        android:name=".MainApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    

    This is a sort of hacky way to do this. There may be better ways. But regardless, you have to pass around the context of the Activity that you want to call finish() on. Then you can close it from anywhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually i m little bit confused in Intent. Suppose i have three activities. A,b,c
I have three activities 1 is the main activity 2 is some other activity
Possible Duplicate: Removing an activity from the history stack Suppose I have three activities
I have three activities, say I have the data from ActivityA, and I want
So, I have three activities/screens. On the first screen, there's a button, that when
I have three activities A, B and C. A starts B with startActivityForResult(getIntent(), ACTIVITY_B);
Ill first tell you what I am doing. I have three activities in the
I have three activities in my android app. First activity is main application screen
I have an application that have three activities, lets call they A, B and
I have three activities in my application ... I use an intent to navigate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.