aspose file tools*
The moose likes Android and the fly likes Multiple Instances of Activity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Multiple Instances of Activity " Watch "Multiple Instances of Activity " New topic
Author

Multiple Instances of Activity

Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 70

Hello Geeks....

I am trying to show the user information on Incoming Call screen, whenever there is an Incoming Call. So i have a Broadcast Receiver listening to Incoming calls, which starts the Intent Service, which subsequently starts an Activity (with Theme Dialog) .

Now Whenever there is an Incoming Call , My Activity Dialog Pops Up and Shows as Intended.

Problem : When the Activity Dialog is already on the Screen and Incoming call comes, there is no new Activity Dialog with new Information. I guess that whenever there is an Instance , android does not creates the new one. So it seems like my problem is "Creating Multiple Instances of an Activity".

Please note that i am starting an Activity from an Intent Service using FLAG_NEW_TASK.

Warm Regards
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Android does create multiple instances of the activity unless you specify the FLAG_ACTIVITY_CLEAR_TOP option. Quoting the documentation for this option.

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.


If you need to receive the new intents on this activity when the clear_top flag is set, override the onNewIntent() method.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 70

@Deepak

Although i can receive the new Intent via overriding the onNewIntent() method, how would i end up having multiple instances of an activity dialog.

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Sahil Rally wrote:@Deepak

Although i can receive the new Intent via overriding the onNewIntent() method, how would i end up having multiple instances of an activity dialog.



When an activity is launched, the android framework creates a stacked history of activities. This is used to allow the user to press the back button and go back to a previous activity. If you do not want this behavior use the FLAG_ACTIVITY_CLEAR_TOP option.

Using FLAG_NEW_TASK will make your activity the first on the history stack. I'm not sure that is what you want.
Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 70

@ Deepak

I want stack of same activity instances . Let us name an Activity as "A".

I want my broadcast receiver to launch new activity instance A whenever there is an incoming call .

So iff there are 5 calls on my phone, i can see 5 activities i.e. Stack : A A A A A

Android documentation says that it is default behaviour but it is not actually happening.

and i am using FLAG_NEW_TASK because am starting activity from an Intent service and it is required , dont exactly know why, but without it ereor comes.
Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Instances of Activity