Android floating view across activities
Asked Answered
T

1

6

Requirement

I have an application with 2 activities, say A and B, with navigations like A->B and B->A (on back press). My requirement is

  • I want a view/layout floating on screen, irrespective of which activity is currently visible. I inflate this view on app start(onCreate of activity A), it remains static on screen during the transition from A->B and when B is onscreen.
  • So naturally this view should be inflated only once (when app starts, onCreate of A).

What I found out

I did some searching, and from what I could find, there are 2 methods to reuse layout in android

  • using <include>

    It just seems like a tool to write xml code of commonly used UI elements. It gets inflated every time it is used in a parent layout.

  • using ViewStub

    I did some research on using ViewStub and It also seems a way to reuse code segment in many layouts. It also need to be inflated every time we use it in a layout except it gets inflated only when we make them visible at run time.

Another hint of my requirement

For people familiar with iPhone development you can add view's to UIWindow, which stays there irrespective of which UIViewController is currently active. I want exact behavior in my application.

My original setup

I am targeting android 2.1 and above. It seems Fragment is available from API level 11 (android 3.0) and above. One option is to use android compatibility library that enables usage of Fragment in older versions. I am currently researching on that now. But I also would like to know if there is any other methods available to fulfill my requirement, rather than change my entire project and use fragments.

I have around 30 odd activities in my application and I want this layout floating over all of them. I just made out a test case with 2 activities to make the question simple and easy.

Topping answered 15/6, 2012 at 6:37 Comment(2)
What does Activity A do that Activity B doesn't and what does Activity B do that Activity A doesn't? Why not just have one Activity with your fixed View and adjust everything else around it?Nedry
OK, I know what you are asking about. In truth I have around 30 odd activities, and it is almost impossible to handle every page inside a single activity. For asking the question I made up a test case with just 2 activity. Sorry, I will edit the question to make that clear..Topping
A
2

Solution 1: FrameLayout

I think what you want to use is the FrameLayout. FrameLayout is designed to block out an area on the screen to display a single item. Child views are drawn in a stack, with the most recently added child on top.

http://developer.android.com/reference/android/widget/FrameLayout.html

Then read here about the back stack that you could use in your activity to flip back and forth between the activities using the back button:

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

Solution 2: Fragment Transactions

Rather than code two separate Activities, code a single Activity with two Fragments. Here is a blurb from the Fragments documentation:

"A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button."

Arrearage answered 15/6, 2012 at 6:48 Comment(6)
Regarding your solution 2 I can't use fragments since fragment require API Level 11 (Android 3.0) and I am targeting API Level 7 (Android 2.1) and above. I specified that in questionTopping
Regarding solution 1, I am not sure I understand. Which activity should I add this FrameLayout? Also see my edit in question (under My original setup).Topping
You can use Fragments from Android 1.6 by including the Compatibility library developer.android.com/sdk/compatibility-library.htmlQuestion
You can use Fragments in Android 2.1 using the compatibility library (I do it all the time and it works great): android-developers.blogspot.com/2011/03/fragments-for-all.htmlArrearage
Thanks, let me go through it.. Still Too bad there is no UIWindow like setup in android..Topping
@jkschneider I edited my question so that the misleading error about Fragment usage in android 2.1 is deleted. Your concern was helpful..Topping

© 2022 - 2024 — McMap. All rights reserved.