Solved

Application open several times

Posted on 2014-03-21
5
226 Views
Last Modified: 2014-03-29
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
Comment
Question by:jpaulino
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 39944826
You need to control your activity lifecycle and use the onStop() method

http://developer.android.com/training/basics/activity-lifecycle/stopping.html
0
 
LVL 48

Author Comment

by:jpaulino
ID: 39945020
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
 
LVL 17

Assisted Solution

by:Chris Harte
Chris Harte earned 100 total points
ID: 39945193
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
 
LVL 48

Accepted Solution

by:
jpaulino earned 0 total points
ID: 39949821
I end up creating a intent service and now it works fine.

Thanks for the ideas.
0
 
LVL 48

Author Closing Comment

by:jpaulino
ID: 39963524
It's required a service to achieve what I want.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If your app took Google’s lash recently, here are the 5 most likely reasons.
Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question