我有一个应用程序,得到了一个活动与2个碎片水平放置。
在我的活动中,我使用oncreateoptionsmenu()
膨胀菜单,使用它我可以显示actionbar选项菜单。但是,当我试图从其中一个片段更新操作栏菜单项时,没有调用oncreateOptionsMenu()
。因此,我无法更新操作栏选项菜单。
package com.andr.androidtablelist;
import java.io.IOException;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.andr.fragments.ListLeftFragment;
import com.andr.utils.DBHelper;
public class TabletActivity extends Activity {
ActionBar actionBar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBHelper dbhelper;
dbhelper = new DBHelper(this);
try {
dbhelper.createDatabase();
} catch (IOException ex) {
ex.printStackTrace();
}
setContentView(R.layout.activity_tablet);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
getFragmentManager().beginTransaction()
.replace(R.id.master, ListLeftFragment.newInstance()).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tablet, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(
getApplicationContext(),
"Back Stack Count::"
+ getFragmentManager().getBackStackEntryCount(),
Toast.LENGTH_LONG).show();
if (getFragmentManager().getBackStackEntryCount() == 1) {
actionBar.setDisplayHomeAsUpEnabled(false);
}
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
// http://www.vogella.com/articles/AndroidListView/article.html
package com.andr.fragments;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import com.andr.androidtablelist.R;
import com.andr.helper.LazyHelper;
public class ListLeftFragment extends Fragment{
private static ListLeftFragment listFragment = null;
public static ListLeftFragment newInstance(){
listFragment = new ListLeftFragment();
return listFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_listlayout, container,false);
getActivity().invalidateOptionsMenu();
ListView lv = (ListView)v.findViewById(R.id.lview);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
getActivity().getActionBar().setTitle("LeftList");
ArrayList<String> list = new ArrayList<String>();
LazyHelper lHelper = new LazyHelper(getActivity());
list = lHelper.getLazyDetials();
/* for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1, list);*/
LeftListAdapter lAdapter = new LeftListAdapter(getActivity(),list);
lv.setAdapter(lAdapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
final String clickedData = (String)arg0.getItemAtPosition(pos);
Bundle bundle = new Bundle();
bundle.putString("clickedItemName", clickedData);
Fragment detailFragment = DetailFormRightFragment.newInstance();
detailFragment.setArguments(bundle);
getActivity().getFragmentManager().beginTransaction().replace(R.id.details, detailFragment).addToBackStack(null).commit();
}
}
);
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.search, menu);
SearchView searchView=(SearchView)menu.findItem(R.id.search);
searchView.setIconifiedByDefault(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
return false;
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
/*MenuItem item;
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.search, menu);
SearchView searchView=(SearchView)menu.findItem(R.id.search);
searchView.setIconifiedByDefault(true);*/
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
private class LeftListAdapter extends BaseAdapter implements OnClickListener {
private Context mcontext;
ArrayList<String> listData;
private LayoutInflater inflater=null;
public LeftListAdapter(Context context, ArrayList<String> listItems) {
mcontext=context;
listData=listItems;
inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup arg2) {
// create a ViewHolder reference
View vi=view;
if(vi==null){
vi = inflater.inflate(R.layout.item, null);
}
TextView text=(TextView)vi.findViewById(R.id.text);
text.setText(listData.get(position));
vi.setOnClickListener(this);
return vi;
}
@Override
public void onClick(View vi) {
TextView text=(TextView)vi.findViewById(R.id.text);
Bundle bundle = new Bundle();
bundle.putString("clickedItemName", text.getText().toString());
Fragment detailFragment = DetailFormRightFragment.newInstance();
detailFragment.setArguments(bundle);
getActivity().getFragmentManager().beginTransaction().replace(R.id.details, detailFragment).addToBackStack(null).commit();
}
}
}
在片段的oncreateview()
方法中,需要调用sethasoptionsmenu(true)
来告诉宿主活动片段有它想要添加的菜单选项。
问题内容: 我知道有人问过这个问题,但是我遵循了所有答案,但仍然有同样的问题。我有两个脚本,一个是片段管理器(IngredientsActivity),另一个是片段(OtherList)。代码如下 成分活动 其他清单 我发现有多个建议可以覆盖onSaveInstanceState的建议,并且我发现更多建议告诉我使用setRetainInstance。我不知道如果要保存列表的值,为什么setReta
我有一个片段,其中有recyclerview,并使用recyclerview适配器在这个recyclerview中设置数据。 现在,我在适配器的列表项中单击了一个按钮,我需要检查android中的READ\u EXTERNAL\u STORAGE权限,以查看android中的新权限模型。 我在这个适配器的片段中创建了一个新函数,用于检查是否授予了权限,如果尚未授予权限,则请求权限。 我已经通过了我
P.P.S@Philipp Jahoda,我实现了onCreateOptionsMenu(...)在片段的托管活动中。
假设有两个Fragments,X和Y。我在X上添加Y片段。当Y完成它的工作,然后删除,X再次可见。 问题:当Y片段被删除并且X再次可见时,我必须执行一些任务,但不会调用,因为它取决于活动。那么我怎么知道片段是可见的呢?
尝试在和中捕获它,如 并且我在中有。
问题是,此AutoResolveHelper不是对片段调用onActivityResult,而是只对Activity调用onActivityResult。 我读过这样的东西: 如果您从片段调用startActivityForResult(),那么您应该调用startActivityForResult(),而不是getActivity().startActivityForResult(),因为这将导