Home > Tricks & Tips > Multiple instances of the same activity are created

Multiple instances of the same activity are created


In my previous post on auto receiving SMS, there’s a problem you can find easily that everytime a new message arrived, a new instance of MainActivity is created (which is a new task launched). It’s kind of wasting memory resources, right?

Here the solution:

1. Add attribute “android:launchMode” for MainActivity in AndroidManifest.xml, I use “singleTask

android:launchMode="singleTask"

2. Add an additiona activity launching flag: FLAG_ACTIVITY_SINGLE_TOP, in order to make it always on top of the screen.

data.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

That’s it!
Hope you learn something!

Cheers,
Pete Houston

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: