0

If I want to display a MapView inside my Activity I would have to extend my Class from MapActivity. If I want to display a Tabbed interface I would have to extend my Class from TabActivity. Similar is the case with some other controls which require user class to extend from a specific class.

Let's say inside my Activity I want to display both a MapView for displaying Google Map and a TabView to display some other data. I can't do it directly because Java doesn't support multiple inheritance. My question is how can I achieve this scenario? How can I display multiple controls inside my activity where each require your class to extend from a specific class. Is it possible first of all? If yes, what are the best practises to achieve this scenario?

Update I want to achieve this

enter image description here

I am using Map and Tab for sake of an example. I would like to know how you can tackle this scenario in general?

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122

2 Answers2

0

You could build it via object composition. Initially I am not sure how to get the Activity started and add it to the layout, but then I found out about LocalActivityManager which allow you to embed other Activity as your view. Note that this class is deprecated since API Level 11. In any case here are the steps to embed other Activity that require extension as a View:

  • Create a LocalActivityManager to enable creation of Activity within Activity
  • Start the activity that you want to embed and get the View via getDecorView()
  • Add the View in your layout

The following is my test code that I tried within my Activity


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create local activity manager so that I could start my activity 
    LocalActivityManager localActivityManager  = new LocalActivityManager(this, true);
    // dispatch the onCreate from this manager
    localActivityManager.dispatchCreate(savedInstanceState);

    // layout to hold the activity, optionally this could be set through XML file
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    this.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

    // start the activity which is in this example is an extension of a TabActivity
    Intent tabIntent = new Intent(this, DummyTabActivity.class);
    Window tabWindow = localActivityManager.startActivity("tabView", tabIntent);
    View tabView = tabWindow.getDecorView();

    // start the activity that extends MapActivity
    Intent mapIntent = new Intent(this, DummyMapView.class);
    Window mapViewWindow = localActivityManager.startActivity("mapView", mapIntent);
    View mapView = mapViewWindow.getDecorView();

    // dispatch resume to the Activities
    localActivityManager.dispatchResume();

    // add to the tabView, optionally you could use other layout as well
    layout.addView(tabView);
    // add to the mapView, optionally you could use other layout as well
    layout.addView(mapView);
}

My limited experiments show that object composition via the above method will achieve what you are trying to do. Having said that, I am not sure how common this approach is. Without your question, I wouldn't probably look for the above method, but your use case is interesting and might be applicable for future use. I will look into Fragment and see if I could do the same with it and update my answer if it is applicable.

momo
  • 21,233
  • 8
  • 39
  • 38
  • You didn't get the point. My question is how to use two conntrols "Side by Side" which require extended class. I know I can put MapView inside TabView "IF" I want my MapView "Inside" MapView – Haris Hasan Oct 02 '11 at 19:01
  • You right, I don't get the question (same as with the other person who just answered). Perhaps you could clarify the question? The way you asked the question seemingly asking how to use TabView and MapView together. Based on your comment, my guess you are asking how to use more than one controls that depend on the extending other classes? – momo Oct 02 '11 at 19:09
  • I just saw your diagram :) and things are clearer now. Doesn't look like there is obvious way to do it (in Android). Let me check and get back to you. – momo Oct 02 '11 at 19:12
  • @HarisHasan I've updated the answer to reflect the question better :) Basically I use Object Composition (which is common) to include the view, the only thing is I don't know how common the approach is using LocalActivityManager. I have not figured out any other way to start an Activity from within another Activity. I will look at the new Fragment API and see if I could update my answer with it. – momo Oct 03 '11 at 23:00
0

In this case it's simple: You can use the MapActivity within the TabActivity (it's designed to manage activities as tabs).

As general approach I always prefer to use views and nest them in an activity. I never use such things like ListActivity. They should make things easier but often look like a bad design decision to me. I never faced the fact that I had to combine two activities (expect TabActivity).

You can take a look at this question. It seems that activities never meant to be used that way. I think the situation which you describe is the reason why fragments where introduced.

Community
  • 1
  • 1
Knickedi
  • 8,742
  • 3
  • 43
  • 45