Solved

Application open several times

Posted on 2014-03-21
5
228 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Introduction Everyone is using a smartphone these days. Contacts, messages, applications, documents, bookmarks, pictures, videos and other stuff which you created and are present only on your device come under the umbrella called “Personal Data”.…
This is an explanation of a simple data model to help parse a JSON feed
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

733 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