Communicating between running activities


SDK Version: 
M3

Starting a new activity from another and passing some data to it is a simple and basic thing in android. But if you want an already running activity to come to foreground, and pass data to it, it can be a bit tricky.

First of all by default if you call an activity with an intent, a new istance of that activity will be created and displayed, even if another instance is already running. To avoid this the activity must be flagged that, it should not be instantiated multiple times. To achieve this we will set the launchMode of the activity to singleTask in the AndroidManifest.xml

  1. <activity android:name="Activity1" android:launchMode="singleTask" android:label="@string/app_name">

This way when we call this activity using an intent, if there is an existing instance, the system will route the request to it. Hoever the onCreate method, where we usually process the passed extraData, will not run this time.

As its name shows it runs when the activity is created and this time it already exists, so the method called onNewIntent() will be called.

  1. protected void onNewIntent(Intent intent) {
  2.   super.onNewIntent(intent);
  3.   setIntent(intent);//must store the new intent unless getIntent() will return the old one
  4.   processExtraData();
  5. }

Do not forget that we can receive data in the normal way in onCreate, when the activity is first created, and since the system can easily kill activities in the backround, if this happens, the onCreate method will be called instead of onNewIntent.

So an elegant solution may call the same function to process extraData from intent from onCreate and onNewIntent.

  1. public void onCreate(Bundle savedInstanceState) {
  2.   super.onCreate(savedInstanceState);
  3.   setContentView(R.layout.main);
  4.   processExtraData();
  5. }
  6.  
  7. protected void onNewIntent(Intent intent) {
  8.   super.onNewIntent(intent);
  9.   setIntent(intent);//must store the new intent unless getIntent() will return the old one
  10.   processExtraData()
  11. }
  12.  
  13. private void processExtraData(){
  14.   Intent intent = getIntent();
  15.   //use the data received here
  16. }

A working example can be downloaded here.

Comments

العاب توم وجيري

العاب بنات

العاب طبخ

العاب سيارات

العاب تلبيس

العاب دراجات

العاب قص الشعر

العاب حرب

العاب سبونج بوب

العاب اطفال

العاب اكشن

العاب بلياردو

العاب باربي

العاب ماريو

العاب كرة القدم

العاب سونيك

العاب بن تن

العاب توم وجيري

العاب فلة

العاب كراش

العاب بنات

العاب باربي

العاب بنات

العاب لولو كاتي

العاب تلبيس

العاب تلوين

العاب ديكور

العاب قص الشعر

العاب طبخ

العاب دوار

العاب براتز

العاب هانا مونتانا

العاب ديزني

اخريطة ي

اخريطة ي

اخريطة ي

العاب اطفال

العاب سيارات اطفال

العاب بنات اطفال

العاب دراجات اطفال

العاب سبونج بوب اطفال

العاب باربي اطفال

العاب تلبيس اطفال

االعاب قص الشعر اطفال

العاب طبخ اطفال

العاب دورا اطفال

العاب ماريو اطفال

العاب سكوبي دو اطفال

العاب

العاب بنات

العاب طبخ

العاب سيارات

العاب تلبيس

العاب دراجات

العاب قص الشعر

العاب حرب

العاب سبونج بوب

العاب اطفال

العاب اكشن

العاب بلياردو

العاب باربي

العاب ماريو

العاب داني الشبح

العاب سونيك

العاب دورا

العاب تلوين

العاب ديكور

العاب بن تن

العاب كرة القدم

العاب مكياج

العاب رياضية

العاب ذكاء

العاب سكوبي دو

دليل رمث

العاب

العاب طبخ

العاب سيارات

العاب قص الشعر

العاب تلبيس

العاب سبونج بوب

العاب سكوبي دو

العاب دراجات

العاب دورا

العاب بنات

العاب حرب

توبيكات

منتديات

The ring pellet mill with ring die is a real specialist in animal feed, It is characterized by large output, less power consumption, that the flat die pellet machine cannot match. Along with other reasons, the machine's speed is lower than its production is obviously less than ring pellet machine, but due to its pressure, it may highly suppress fine feed.

These kind of posts are always inspiring and I prefer to read quality content so I am happy to find out many good point here in the post, writing is simply great, thank you for the post.

website design company | hire joomla designer

Is this a similar process to what is done in other apps, such as Endomondo?

The Nefarious NapStealers Thank You

This gives very fortunate information it help a lot

thanks
cna classes

Asics has sung about style many times before, including a 2009 song called ‘Fashion,’ which appeared on the ‘Confessions of a Shopaholic’ soundtrack and mentioned several of her favorite designers Aics Running Shoes, including McQueen.Asics Running Shoes But ‘Fashion of His Love’ doesn’t recycle the Asics onitsuka tiger themes from that track.Asics Shoes Instead, it focuses on being a committed relationship, while also mentioning the late designer Asics Shoes UK, who greatly influenced Gaga’s sense of style. “You know that I’d never cheat on a man, cause I’m not like that,” Gaga sings.Asics Revolve LE “I’m physically crafted to be as fitting as Asics UK McQueen.”

thanks for sharing this info with us central playa hotel ibiza

I've been working on this for a while now, and this helps a ton. Thanks!

Nefarious Napstealers

Drupal Themes
We are the leaders in Drupal Web Development services that has established itself as a sought after Website Design and Development company servicing business of all sizes.

Very good articles, you are inspiration.
Congratulations again
and good luck for the future.

voyance web
voyance audiotel
voyance immediate
meduim
meduim spirit
voyance par telephone

The burberry outlet online brand name is a well-known high class fashion brand.When you're looking for quality, fashion, and prestige, burberry outlet stores is the best choice.Fashion is also a top priority with burberry outlet designs.Go to the burberry outlet store,and choose your favourite things. burberry coats

A communications protocol is a formal description of digital message formats and the rules for exchanging those messages in or between computing systems and in telecommunications.punjabi sms

can I use this code for developing app for mobile web?

Dilantha
Electrical Engineering Questions

Interesting info
thanks

bb