0

I'm new to Android Java programming.

I've created a simple chat app. Things are going well, but I've hit a small snag. I've set launchMode="singleTop" in my manifest's application tag, and overrided the back button, so now the chat app remains running (in the background) when the user presses the back or home button. I've also created some notifications that pop up in that status bar when a new chat appears (with intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP) set), and when the user clicks the notification it brings the already running chat app to the front. That's all working fine; the problem arises after I bring up the app from the status bar notification, then click back or home, then choose to open the app from the applications menu or a shortcut on the launcher, and thats when it opens up a second instance of the app.

Is that clear?

If the app isn't running and I choose to run it from app menu, it runs (doesn't display any chats because it hasn't received any). I receive a chat and see it on screen in the app, I press back button, I go back and open app through app menu, it runs and has the chat messages from before. Good. Now I press back (so app is in background, but still running), I receive a chat, it pops up a status bar notification, I click it, it opens up the app and shows chat messages from before. Good. Now I click back (so app is running in background). I then open the app through the app menu (like I did the first and second time), but this time it doesn't show the previous chat messages, and if I receive a chat, it makes 2 notification sounds, so I know it has started a new instance of the app.

Any idea what's wrong?

Thanks.

SvEnjoyPro
  • 764
  • 1
  • 9
  • 12
  • poss dup http://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ – L7ColWinters Apr 12 '12 at 20:17
  • Thanks for the response. I tried adding an isTaskRoot() check and it returns true every time except the last (when the system creates a second instance), but I launched the app from the app menu. Any idea why that would be, and how to prevent it? – SvEnjoyPro Apr 12 '12 at 20:38
  • What is wrong? Well, You should use Service for internet connection (reciving messages, sending, ...) and Activity only for showing messages – Selvin Apr 12 '12 at 20:41
  • From what I've read, you are correct Selvin, i should use a service, I'm just really new to all this and it was one more thing to learn when what I currently have is almost working. Do you know of any good service tutorials? I looked at dev.android.com, but that was confusing for me, I also looked at some tutorials from google, but none of them seemed very simple. Thanks for all your help. – SvEnjoyPro Apr 12 '12 at 20:47

1 Answers1

3

set the activity launchMode to singleInstance in manifest.

San
  • 5,567
  • 2
  • 26
  • 28