• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Application open several times


I have a small application (contdown timer), using threads, that works fine, except when you use the back button in (hardware.

Here is the problem:

The application starts a timer and shows it in the layout (RelativeLayout) and when you press the back button it leaves to home screen (and keeps running).

Then you tap the icon to open again the application and it shows the timer running. But if you press again the back button, then the next time you tap the icon, it starts a new instance.

This is what I have:

Android Manifest:
        <activity 	android:name=".MainActivity"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Open in new window

And in the code:

	protected void onCreate(Bundle savedInstanceState) {

Open in new window

Any ideas?

Jorge Paulino
Jorge Paulino
Jorge Paulino
  • 3
  • 2
2 Solutions
Chris HarteThaumaturgeCommented:
You need to control your activity lifecycle and use the onStop() method

Jorge PaulinoIT Pro/DeveloperAuthor Commented:
Thanks for the quick reply, but I don't want to save the state ... I just want to show again my running application.

The application starts a timer that does some work periodically and I don't want to start a new app. The second time I hit the back button it destroys it.

Maybe I can override the back button to don't destroy the application, but I don't know if it's the correct approach.
Chris HarteThaumaturgeCommented:
I think what you want it the onPause() and onResume() methods. This will keep an activity until you want it again, it will then open it without starting a new instance.


You can override the back button that is a big deal, and interferes with the operation of the device.
Jorge PaulinoIT Pro/DeveloperAuthor Commented:
I end up creating a intent service and now it works fine.

Thanks for the ideas.
Jorge PaulinoIT Pro/DeveloperAuthor Commented:
It's required a service to achieve what I want.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now