开发者

Best practise for displaying multiple controls which require an extended class?

开发者 https://www.devze.com 2023-04-09 19:34 出处:网络
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

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 Ja开发者_运维问答va 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

Best practise for displaying multiple controls which require an extended class?

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消