我正在使用支持操作栏。我也在使用片段。通常,我使用导航抽屉更改片段,一切正常。每个片段都可以更改操作栏项。当我更改为新片段时,将删除更改(新按钮)。
这是我的问题。在一个实例中,我替换片段并将现有片段添加到后栈。当我回到上一个片段时,操作栏仍然包含前一个片段中的项目。
实施例: 1)片段1加载片段2并将片段1放在后栈上 2)片段2在操作栏中放置了几个新按钮 3)片段2退出,现在我看到片段1和片段中的操作栏项目2
我在另一个位置使用其他2个片段执行相同的基本操作,并且没有相同的问题。我可以在两个实现之间找出的唯一区别是第二个片段与操作栏交互的方式。
在工作案例中,片段2加载项目使onCreateMenuOptions中的菜单膨胀。这很有效。
在NOT工作情况下,片段2不会使菜单膨胀,而是使用自定义视图:
// Set action bar custom view and display options.
MINMainActivity.getSharedInstance().getSupportActionBar().setCustomView(R.layout.custom_action_bar_viewer);
MINMainActivity.getSharedInstance().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO);
MINMainActivity.getSharedInstance().getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_bg));
这是我能找到的唯一区别。我错过了什么吗? (显然)但问题是什么???
用于从片段1加载片段2的代码是:
MINMainActivity.getSharedInstance().mDrawerToggle.setDrawerIndicatorEnabled(false);
// Load the PDF fragment
MINPDFTronFragment fragment = new MINPDFTronFragment();
fragment.albumItem = albumItem;
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
用于片段2的onCreate代码是:
// ***************************************************************************************************
//
// onCreate
//
// ***************************************************************************************************
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//MINMainActivity.getSharedInstance().supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY);
setRetainInstance(true);
setHasOptionsMenu(true);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
新编辑:
这些是在操作栏上执行工作的两种方法。这里的第一部分是片段1的代码。片段1是启动片段2并将其自身放在后台上的片段:
// ***************************************************************************************************
//
// onCreateOptionsMenu
//
// ***************************************************************************************************
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
mMenu = menu;
// Inflate GridView menu
inflater.inflate(R.menu.gridview_combined_menu, menu);
// Gridview share menu
MenuItem shareMenuItem = menu.findItem(R.id.MenuItemShare);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
setGridViewShareIntent(null); // TODO is this needed?
super.onCreateOptionsMenu(menu, inflater);
}
// ***************************************************************************************************
//
// onPrepareOptionsMenu
//
// ***************************************************************************************************
@Override
public void onPrepareOptionsMenu(Menu menu)
{
setOptionMenuItems();
super.onPrepareOptionsMenu(menu);
}
以下是Fragment 2中的相同方法(Fragment 1发布的片段:
// ***************************************************************************************************
//
// onCreateOptionsMenu
//
// ***************************************************************************************************
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
mMenu = menu;
super.onCreateOptionsMenu(menu, inflater);
}
// ***************************************************************************************************
//
// onPrepareOptionsMenu
//
// ***************************************************************************************************
@Override
public void onPrepareOptionsMenu(Menu menu)
{
MINMainActivity MINMainActivity = (MINMainActivity) getActivity();
boolean drawerIsOpen = MINMainActivity.mDrawerLayout.isDrawerOpen(MINMainActivity.mDrawerList);
if(drawerIsOpen)
{
}
else
{
}
}
super.onPrepareOptionsMenu(menu);
}
如您所见,Fragment 2并未在这些方法中制作mod。下面是片段2中操作操作栏的代码:
// ***************************************************************************************************
//
// onCreateView
//
// ***************************************************************************************************
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
initViewerLayout(inflater, container, savedInstanceState);
}
// ***************************************************************************************************
//
// initViewerLayout
//
// ***************************************************************************************************
private void initViewerLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Initialize the library
try
{
PDFNet.initialize(MINMainActivity.getSharedInstance(), R.raw.pdfnet);
}
catch (PDFNetException e)
{
// Do something...
e.printStackTrace();
}
// Inflate the view and get a reference to PDFViewCtrl
pageView = inflater.inflate(R.layout.pdf_viewer, container, false);
// Set action bar custom view and display options.
MINMainActivity.getSharedInstance().getSupportActionBar().setCustomView(R.layout.custom_action_bar_viewer);
MINMainActivity.getSharedInstance().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO);
MINMainActivity.getSharedInstance().getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_bg));
// Initialize action bar buttons (we are using a custom view, so we
// need to set click listeners for each item.
mButtonViewMode = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_viewmode);
mButtonViewMode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewModePickerDialogFragment dialog = new ViewModePickerDialogFragment();
dialog.mListener = MINPDFTronFragment.this;
dialog.show(MINMainActivity.getSharedInstance().getSupportFragmentManager(), "view_mode_picker");
stopHideToolbarsTimer();
}
});
updateButtonViewModeIcon();
mButtonOutline = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_outline);
mButtonOutline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = MINMainActivity.getSharedInstance().getSupportFragmentManager();
// Creates the dialog in full screen mode
mBookmarksDialog = new BookmarksDialogFragment(mPdfViewCtrl, mBookmarkDialogCurrentTab);
mBookmarksDialog.setBookmarksDialogFragmentListener(MINPDFTronFragment.this);
mBookmarksDialog.mOutlineDialogFragmentListener = MINPDFTronFragment.this;
mBookmarksDialog.mAnnotationDialogFragmentListener = MINPDFTronFragment.this;
mBookmarksDialog.setStyle(DialogFragment.STYLE_NO_TITLE, android.support.v7.appcompat.R.style.Theme_AppCompat_Light);
mBookmarksDialog.show(fragmentManager, "bookmarks_dialog");
stopHideToolbarsTimer();
// Example of how to show the OutlineDialogFragment as a dialog
//OutlineDialogFragment outlineDialogFragment = new OutlineDialogFragment(mPdfViewCtrl);
//outlineDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light);
//outlineDialogFragment.show(fragmentManager, "outline_dialog");
// Example of how to show the AnnotationDialogFragment as a dialog
//AnnotationDialogFragment annotDialog = new AnnotationDialogFragment(mPdfViewCtrl);
//annotDialog.setStyle(DialogFragment.STYLE_NO_TITLE, android.support.v7.appcompat.R.style.Theme_Base_AppCompat_Light);
//annotDialog.show(fragmentManager, "annotation_dialog");
}
});
mSearchView = (SearchView) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_search);
mSearchView.setFocusable(false);
mSearchView.setFocusableInTouchMode(false);
// Let's get the internal button of the search view widget to change some properties.
ImageView searchButton = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchButton.setBackgroundDrawable(mSearchView.getContext().getResources().getDrawable(R.drawable.controls_annotation_toolbar_tool_bg));
searchButton.setImageDrawable(mSearchView.getContext().getResources().getDrawable(R.drawable.ic_action_search));
mSearchView.setOnQueryTextListener(this);
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SearchView searchView = (SearchView) view;
if (searchView.isIconfiedByDefault() && !searchView.isIconified()) {
AnalyticsHandler.getInstance().sendEvent("[Viewer] Find Text clicked");
// Search has expanded
startSearchMode();
}
}
});
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
exitSearchMode();
return false;
}
});
mButtonAnnotToolbar = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_tools);
mButtonAnnotToolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnnotationToolbar = new AnnotationToolbar(MINApplication.getAppContext(), pageView, mToolManager);
mAnnotationToolbar.setButtonStayDown(SettingsManager.getContinuousAnnotationEdit(MINApplication.getAppContext()));
if (SettingsManager.getContinuousAnnotationEdit(MINApplication.getAppContext())) {
AnalyticsHandler.getInstance().sendEvent("[General] Continuous Annotation Edit enabled");
}
mAnnotationToolbar.show();
AnalyticsHandler.getInstance().sendEvent("[Annotation Toolbar] Annotation Toolbar opened");
}
});
mButtonShare = (ImageButton) MINMainActivity.getSharedInstance().getSupportActionBar().getCustomView().findViewById(R.id.ab_action_share);
mButtonShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (MiscUtils.isKitKat()) {
// If KitKat or higher, we show the Share/Print option
SharePickerDialogFragment dialog = new SharePickerDialogFragment();
dialog.mListener = MINPDFTronFragment.this;
dialog.show(MINMainActivity.getSharedInstance().getSupportFragmentManager(), "share_mode_picker");
} else {
MiscUtils.sharePdfFile(MINApplication.getAppContext(), albumItem);
AnalyticsHandler.getInstance().sendEvent("[Viewer] Share clicked");
}
}
});
// Search prev/next buttons.
mButtonSearchNext = (ImageButton) pageView.findViewById(R.id.floating_button_next);
mButtonSearchNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSearchUp = false;
findText();
}
});
mButtonSearchPrev = (ImageButton) pageView.findViewById(R.id.floating_button_prev);
mButtonSearchPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSearchUp = true;
findText();
}
});
// PDFViewCtrl settings
mPdfViewCtrl = (CompleteReaderPDFViewCtrl) pageView.findViewById(R.id.pdfviewctrl);
mPdfViewCtrl.setUrlExtraction(true);
mPdfViewCtrl.setupThumbnails(true, true, true, 0, 50 * 1024 * 1024, 0.1);
mPdfViewCtrl.setPageViewMode(PDFViewCtrl.PAGE_VIEW_FIT_PAGE);
mPdfViewCtrl.setPageRefViewMode(PDFViewCtrl.PAGE_VIEW_FIT_PAGE);
mPdfViewCtrl.setHighlightFields(true);
mPdfViewCtrl.setZoomLimits(PDFViewCtrl.ZOOM_LIMIT_RELATIVE, 1.0, 20.0);
updateViewMode();
mSeekBar = (ThumbnailSlider) pageView.findViewById(R.id.thumbseekbar);
// We pass a reference of the PDFViewCtrl to the slider so it can
// interact with it (know number of pages, change pages, get thumbnails...
// At this point no doc is set and the slider has no enough data
// to initialize itself. When a doc is set we need to reset its data.
mSeekBar.setPDFViewCtrl(mPdfViewCtrl);
mPageNumberIndicator = (TextView) pageView.findViewById(R.id.page_number_indicator_current_page);
mSearchProgressDialog = new ProgressDialog(getActivity());
mSearchProgressDialog.setMessage(getResources().getString(R.string.search_in_progress));
mSearchProgressDialog.setIndeterminate(true);
mSearchProgressDialog.setCancelable(true);
mSearchProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mPdfViewCtrl.cancelFindText();
}
});
mDownloadDocumentDialog = new ProgressDialog(getActivity());
mDownloadDocumentDialog.setMessage(getResources().getString(R.string.download_in_progress_message));
mDownloadDocumentDialog.setIndeterminate(true);
mDownloadDocumentDialog.setCancelable(true);
mDownloadDocumentDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(MINApplication.getAppContext(), R.string.download_cancelled_message, Toast.LENGTH_SHORT).show();
//TODO finish();
}
});
}
答案 0 :(得分:9)
尝试拨打1片段的onResume
中的invalidateOptionsMenu()。
编辑:
如果在片段2中设置了自定义操作栏,则需要在第一个片段中禁用它(例如,在onResume()
中):
MINMainActivity.getSharedInstance().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO);
答案 1 :(得分:0)
这是我的问题。在一个实例中,我替换片段并将现有片段添加到后栈。当我回到上一个片段时,操作栏仍然包含前一个片段中的项目。 您使用新按钮更改操作栏的呼叫方法?如果你在片段onCreate()中调用它,它将不会被调用。尝试从片段onActivityCreated(Bundle savedInstanceState)调用它。
看一下片段生命周期方法