我正在使用自定义工具栏。我需要添加回按钮。现在我使用此代码添加后退按钮。
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.white));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back_arrow));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
这很好用。我可以看到添加了后退按钮。但请考虑我在Fragment1中没有后退按钮的情况。现在我转到Fragment2,然后添加Back Button。从片段2我打开片段3然后我再次添加后退按钮。
现在,当我按下fragment3中的后退按钮返回到fragment2时,我必须检查片段堆栈以查看片段2中是否需要后退按钮。
当我们将片段推送到堆栈时,还有其他方法可以自动处理后退按钮吗?
答案 0 :(得分:18)
只需添加两行新代码即可。像这样的东西
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.white));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back_arrow));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
答案 1 :(得分:8)
这假设您使用的是AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
然后在onOptionsItemSelected中,您可以按如下方式覆盖主页按钮:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if(id == android.R.id.home){
Intent i= new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
答案 2 :(得分:0)
您可以非常轻松地处理后退图标。如果你的所有片段都在单个Activity中,我真的建议用以下方式处理它:
首先创建一个实现FragmentManager .OnBackStackChangedListener
的抽象BaseFragment类,然后在其中放入以下方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivity = (MainActivity) getActivity();
getFragmentManager().addOnBackStackChangedListener(this);
shouldDisplayHomeUp();
}
@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
public boolean shouldDisplayHomeUp() {
//Enable Up button only if there are entries in the back stack
boolean canBack = false;
try {
canBack = getFragmentManager().getBackStackEntryCount() > 0;
} catch (Exception ex) {
// Log.e(getClass().getCanonicalName(), ex.getMessage());getMessage
}
if (canBack) {
mainActivity.drawerDisable();
} else {
mainActivity.drawerEnable();
}
return canBack;
}
通过这种方式disableDrawer
& enableDrawer
函数处理您的Icon,OnBackPressed
方法在您的活动中处理BackStack Now,如果需要,按下后退图标显示。你的onBackPressed
应该是这样的:
int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
if (backStackCount == 0) {
//nothing exist in backStack OS handle it
super.onBackPressed();
} else {
getSupportFragmentManager().popBackStack();
}
请参阅完整实施here。
答案 3 :(得分:0)
在课程中使用方法
private void setupToolbar(){
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
答案 4 :(得分:0)
它适用于工具栏的后退功能
private setUpToolBar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
答案 5 :(得分:0)
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
这很好,如果您有片段,请创建一个onBackPressed()方法
@Override
public void onBackPressed() {
int position = mViewPager.getCurrentItem();
if(position == 2) { // go back to search / result tab from info detail tab
mViewPager.setCurrentItem(2);
} else if(position == 0) { // switch from result to search tab or go back to home tab
SectionsPagerAdapter sectionsPagerAdapter = (SectionsPagerAdapter) mViewPager.getAdapter();
Fragment fragment = sectionsPagerAdapter.getItem(position);
if(fragment instanceof ResultFragment) {
Bundle bundle = ((ResultFragment) fragment).getArguments();
if(bundle != null) {
sectionsPagerAdapter.replaceFragment(SearchFragment.newInstance(bundle.getString(GlobalInfo.TAG_ID), bundle.getString(GlobalInfo.PART_NO), bundle.getString(GlobalInfo.SERIAL_NO), bundle.getString(GlobalInfo.PART_NAME)), getString(R.string.search), 0);
}
} else {
mViewPager.setCurrentItem(1);
}
}
else if(position == 3){
SectionsPagerAdapter sectionsPagerAdapter = (SectionsPagerAdapter) mViewPager.getAdapter();
Fragment fragment = new ToolMgtFragment();
sectionsPagerAdapter.replaceFragment(fragment,"Tool Mgt", 3);
}
else {
super.onBackPressed();
}
}