File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes Activity Instance always on top of its Activity Stack. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Activity Instance always on top of its Activity Stack." Watch "Activity Instance always on top of its Activity Stack." New topic
Author

Activity Instance always on top of its Activity Stack.

Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 70

Hello Geeks


Problem : I want one "Dialogue Activity " to be always on the TOP of my Application . Let us say Activities as A1 ,A2 and so on... and Dialog Activity as D

Now Application starts and A1 is shown to the user. Now A1 launches (in onCreate() ) D activity on top of it. Since D activity is just a Dialog Activity showing some information to the user. It is on one side of the screen and is draggable so that user can move it to any part of the screen. At this time ,user can still can see A1 and touch it. So now user goes to Activity A2 from A1 ( By pressing a button in A1) .


Requirement : Now i want is that the D activity comes over A2 automatically. Now when user launches A3, D comes over A3 and so on so forth.....

So Activity Task movement is as follows


A1 D
(Launch A2 from A1)
A1 A2 D
(Launch A3 from A2)
A1 A2 A3 D


How could i achieve the same ?

Warm Regards
D. Smith
Author
Greenhorn

Joined: Jan 11, 2013
Posts: 25
    
    5

Android doesn't really provide you the control over the Activity stack to do anything like this directly. If you are allowing a user to interact with a paused Activity while it is in that state underneath another Dialog themed Activity (a pattern that really isn't recommended), then you will have to implement logic in your application to finish the current "Dialog" and re-display it again after the new Activity has launched (persisting its state somewhere so it can show up looking the same as it did before you closed it). The "Dialog" can either finish itself when it detects that it has been paused (because your new A2 or A3 has come over the top of it) and that new Activity can be responsible for launching a new version of the "Dialog" in onCreate() ...OR... You might be able to use the finishActivity() method from the paused Activity to force finish the "Dialog" before launching the next Activity, which will then automatically re-display the "Dialog".

The above is all very hacky because it truly is using the Activity stack in a way that is was not designed to be used. Another approach you might consider is to implement this application with Fragments instead. Using Fragments, you can swap out (using the replace() method) the screen section underneath because you define the container view location where a Fragment hierarchy should load. The "Dialog" can also be a Fragment that just displays itself inside of a different container location.


Android Recipes: A Problem Solution Approach: http://www.apress.com/9781430246145
Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 70

Thank-you D.Smith for very clear explanation.

I heard about certain FLAGS which can trick this thing as well, something related to re-ordering of Activities in Stack or poping some activity to the top. Cannot that trick would help ??

Warm Regards
D. Smith
Author
Greenhorn

Joined: Jan 11, 2013
Posts: 25
    
    5

You can use Intent flags to manage how a new Activity is launched; things like pulling an existing instance from within the stack rather than creating a completely new one, or modifying how the new instance is tracked in the stack history. However, there is no flag that allows you to insert a new Activity in between two existing Activity instances in the stack...the new Activity you launch will always be on the top of the stack. Allowing this behavior at the framework level has too many consequences that can break result delivery chains that may exist from one instance to another.
Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 70

Okayy great , thanks for the prolific information . Appreciate.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Activity Instance always on top of its Activity Stack.