I don’t seem to understand if it’s possible to inflate (include) an activity into another activity. I know i can inflate a layout xml, this works, but i am wondering if i can inflate an activity. For instance , i have class A that extends Activity and another class B that extends ListActivity. Can i include and use in class A, my class B?
THis is what i have tried:
Class A:
LayoutInflater inflater = (LayoutInflater) MyActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate list
BActivity list = new BActivity();
Class B:
public class BActivity extends ListActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
List<Model> models= new ArrayList<Model>();
models.add(new Model("John"));
models.add(new Model("Cage"));
setListAdapter(new MyAdapter(this, models));
ListView list = getListView();
}
}
and in xml (the class A xml): (for where i want to see the list)
<view class="com.test.BActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > </view>
All of this throws errors :
Error inflating class BActivity
The activities are declared in the manifest.
Do you know what i am doing wrong? this is not the correct way to inflate another activity? I am using Android 2.2 api 8.
Thank you for your time.
Your question title and your issue are not actually the same thing. For completeness, I will answer both.
The answer is there is no difference. Ultimately, they are the same in process and logic. However, an Activity may have many different Views and you may
setContentView()several times to several different Layouts or Views based on your need. An Activity requires a Layout resource, and a View may or may not be a Layout.Yes. Absolutely.
BActivity list = new BActivity();is not actually inflating an Activity. You are constructing the Activity, but not starting it.BActivityas aView, but your code defines it as anListActivity. These are two different things entirely. AListActivityhas aListView(extended or otherwise); AListActivityis not aListView.Activityand its subclasses areContextsthat have a Life Cycle that is managed by the OS. They contain and speak toViewsof all types, but are not themselves Views.No sir, but fear not! The answer is not too far away.
FAKE ANSWER (for completeness) –
First, to start another Activity so that it is inflated, you must call
startActivity()from aContext. AContextmay be an Application, Activity, Broadcast Reciever or any other app component (Component = declared object in your Android project manifest). So, if you really wanted to start a new Activity, you would changeBActivity list = new BActivity();to:REAL ANSWER –
However, since you want to see your List in class A, BActivity is not an Activity, it is a View. That means what you REALLY want is to make it recognize your View and this is a different solution. Change
public class BActivity extends ListActivitytopublic class BActivity extends ListViewand now all of a sudden you have a custom View!! Now all we have to do is get the List to work.Constructing the View – Views are different from Activities in that they do not have a
public void onCreate(Bundle bundle). All of your stuff fromBActivity.onCreate()would instead be placed in the constructor. But, you don’t have a proper constructor… hmmm. Well, there are three constructors to choose from — add one or all of the following (You will probably want either option 1 or 2, at first. But you won’t use both at the same time hint hint, read the comments:Inflating the Activity = Inflating the View
You have a choice here, you can either add the View, or you can inflate the View. There are many options for both. Based on your question, I shall assume you want to inflate the View. Simply change
BActivity list = new BActivity();tosetContentView(R.id.MyXML). MyXML, of course, would be the name of your XML Layout file. SetContentView will then open the appropriate View for you (BActivity) using the 2nd constructor from the list above.Understanding the difference between View and Activities is important. The processes between them are very similar, but they themselves have a intertwined but separate purpose.
LayoutInflaterActivityhas a convenience method calledsetContentViewwhich can inflate an entire XML file.LayoutInflater object.inflate().For more information, certainly read more on the Android Developers Resources. However, some of these things are only learned by experimentation.
Hope this all helped!
FuzzicalLogic