如何在我的Android应用程序上创建一个条件当我按下工具栏上的后退按钮时会发生一些代码。
我试过了,但它不起作用。
@Override
public void onBackPressed() {
super.onBackPressed();
stopTimerTask();
}
答案 0 :(得分:1)
试试这个:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// this takes the user 'back', as if they pressed the left-facing
triangle icon on the main android toolbar.
// if this doesn't work as desired, another possibility is to call
`finish()` here.
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 1 :(得分:0)
Toolbar toolbar;
toolbar.setNavigationIcon(R.drawable.left_arrow_thick);
setSupportActionBar(mToolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//write your code here
stopTimerTask();
}
});
答案 2 :(得分:0)
以下代码使用: -
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_nav_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("cek", "home selected");
//do something here
}
});
答案 3 :(得分:0)
工具栏操作。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setSubtitleTextColor(Color.WHITE);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setBackgroundColor(getResources().getColor(
R.color.themeToolbarColor));
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});