Solved

Application open several times

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

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 16

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I tend toward trying the newest hardware and software.  Thiss sometimes works out to my benefit, and sometimes not.  Because I downloaded and installed Android 5.x (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.htm…
If your app took Google’s lash recently, here are the 5 most likely reasons.
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.

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now