monkey.jsun wrote: > I created a single-activity application that spawns a thread and binds > to a socket. For demo purpose, I need to switch to other application > through "return to home" key. After that I like to switch back to the > original application. > > I tried to click on my application again. Apparently the onCreate() is > invoked again, and the newly spawned thread will fail to bind to the > socket. That, (plus the output from "ps"), seems to suggest the > previous instance still exists. > > How can I go back to the previous instance?
If by "click on my application again", you mean you clicked on your icon in the launcher, by default that should create another instance of your activity process. In that respect, the launcher works the same as launchers in other OSes. The "home" key brings up the Launcher activity and puts it at the top of the activity stack. Pressing the back button should eventually return you to your existing activity instance. If you only want at most one instance of your activity running, you probably want to take a look at the android:launchMode attribute of the <activity> element of your AndroidManifest.xml file: http://code.google.com/android/reference/android/R.styleable.html#AndroidManifestActivity_launchMode You'd want to set android:launchMode to singleTop, singleTask, or singleInstance, depending on what you're trying to achieve. > Alternatively, I was thinking whenever my application becomes > invisible I can try to terminate my application completely, instead of > letting hang around and causes problem next time I click the app > again. How do I do that? If you don't need the socket connection when you're not visible, I'd drop the socket in onPause() and bind in onResume(): http://code.google.com/android/reference/android/app/Activity.html#ActivityLifecycle If you really want to close your activity, call finish() on the activity, and that will have it close up shop. -- Mark Murphy (a Commons Guy) http://commonsware.com The Busy Coder's Guide to Android Development -- coming in June 2008! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] Announcing the new M5 SDK! http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

