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

Application open several times

Hi,

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"
            		android:label="@string/app_name" 
            		android:screenOrientation="portrait"
            		android:launchMode="singleTask" 
            		android:configChanges="keyboard|keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Open in new window


And in the code:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		initializeEvents();
	}

Open in new window


Any ideas?

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

http://developer.android.com/training/basics/activity-lifecycle/stopping.html
0
 
jpaulinoAuthor 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.
0
 
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.

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

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

Thanks for the ideas.
0
 
jpaulinoAuthor Commented:
It's required a service to achieve what I want.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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