Option Menu not opening from ListActivity when list is empty, if the ListActivity is started from within an ActivityGroup
This is a follow up to a question I posted last week ( Option Menu not opening from ListActivity when list is empty .) I have done some work to narrow down the problem I have been having. The problem with the option menu is rather convoluted and not for the faint at heart.
My project uses as ListActivity with an Option Menu. The ListActivity is started from within an Activity Group. (I use it inside an activity group, so that the "back" button will work properly for moving backwards through activities within a particular tab.)
The Option Menu works perfectly, as long as there is data supplied for the list. However, if there is no data, the option menu button will not trigger the option menu.
The Option Menu does, however, work for a ListActivity with an empty list, provided the ListActivity is not started from within an ActivityGroup.
To recreate the problem, I have created a very very stripped down example using a ListActivity class (MsgList) started from within an ActivityGroup class (TabsFirstGroup.)
If I comment out line 30 in MsgList (where I populate the array that supplies the data for the list, words.add(s);
) the options menu will fail to instantiate. I never reach the onCreateOptionsMenu() method inside MsgList, as is deduced through setting a debug break point that is never hit.
Here is the TabsFirstGroup class: package com.techmeridian.testframework;
import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class TabsFirstGroup extends ActivityGroup {
// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
public static TabsFirstGroup group;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
View view = getLocalActivityManager().startActivity("MsgListActivity", new
Intent(this,MsgList.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
}else {
finish();
}
}
@Override
public void onBackPressed() {
TabsFirstGroup.group.back();
return;
}
}
Here is the MsgList class:
package com.techmeridian.testframework;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.ArrayAdapter;
public class MsgList extends ListActivity {
private static final String[] items={"lorem", "ipsum", "dolor",
"sit", "amet", "consectetuer", "adipiscing", "elit",
"morbi", "vel", "ligula", "vitae", "arcu", "aliquet",
"mollis", "etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};
private ArrayList<String> words=null;
@Override
protected void onCreate(Bundle savedInstanceState)开发者_C百科 {
super.onCreate(savedInstanceState);
words=new ArrayList<String>();
for (String s : items) {
// IF I COMMENT THE NEXT LINE OUT, THEN THE OPTION MENU WILL NOT WORK!
words.add(s);
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, words));
registerForContextMenu(getListView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
}
Any assistance would be greatly appreciated. Thanks.
精彩评论