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 8963749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:25:37+00:00 2026-06-15T16:25:37+00:00

I have an activity that is started from another activity via a button and

  • 0

I have an activity that is started from another activity via a button and which gets passed an intent when started.
I now figured out, that the activity is then created several times, since it is started with an intent, and will call onCreate() every time I hit the button mentioned above. In the documentation I read it says “Every time there’s a new intent for a “standard” activity, a new instance of the class is created to respond to that intent”, so that I assume that I then have several instances of that activity somewhere on the stack. This is what I would like to avoid, since the activity has some static variables which are referenced from other activites and I want to make sure that the value of this variable is deterministic!

What I want is to get rid of all existing instances and create a new instance of the activity when I press the button, or make sure to have just one activity and create it with the new intent everytime.

I tried to achieve this by setting android:launchMode=”singleTop” for the activity and implementing onNewIntent(intent). This should make sure that I only have one instance of the activity in this scenario and I get the new intent. However if I do it like this, I basically have to copy my whole onCreate() method, but just using the new intent, which feels wrong.

public void onNewIntent(Intent intent) {
    myAdapter= null;
    serverThread.stopThread();
    serverThread = null;
    this.onCreate(...);  // <- somehow do all this with new intent
}

Do you have a good idea how to solve this situation easily?

  • 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-15T16:25:38+00:00Added an answer on June 15, 2026 at 4:25 pm

    In the above situation actually onDestroy() is called when the user is navigating back to the previous activity with the button creating the intent and starting the activity. I.e. actually in this case there is already only one instance of the activity and the previous one is being destroyed.

    For this reason onNewIntent() is not called in the above setup. If you want to e.g. stop a server, as indicated above in the question by a piece of code, so that you can start a new one when pressing the button again, you hence can actually simply do this:

    @Override
    public void onDestroy() {
        serverThread.stopThread();  
        super.onDestroy();
    }
    

    Hope that helps others 🙂

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

Sidebar

Related Questions

I have an activity that I'd like to load from a preference screen. I'm
I have an activity that launches another activity with startActivityForResult method. I would like
I have a problem on going back (clicking the Back button) from sub-activity to
I have started Activity for result, but how to return string like parameter from
I have two Android notifications that get started from my main (not at the
I have an Activity that plays some audio from within a ListActivity . The
I have a legacy web app that I am maintaining. It started out as
I have a service running in background. I started it from an Activity, but
I have a welcome screen that can be started as the first activity in
I have an application without launcher activity that works properly from Android 1.5 to

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.