Solved

Application open several times

Posted on 2014-03-21
5
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
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…

695 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