我有以下活动代码: -
public class legislator_info extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_legislator_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Legislator Info");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
String bioguide = i.getExtras().getString("Person");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// click on 'up' button in the action bar, handle it here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
所以基本上我有一个片段,里面有一个列表视图。单击列表项目我开始此活动,我想回到上一个片段的后退按钮单击。我尝试了上面的代码,但我无法返回。非常新的任何帮助表示赞赏。
我已按以下方式添加了我的片段: -
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
LegislatorFragment lf = new LegislatorFragment();
ft.replace(R.id.fragment_container,lf);
ft.addToBackStack(null);
ft.commit();
答案 0 :(得分:1)
我仍然不清楚你想要实现什么,但你可以试试这个
argv[argc - 1]
编辑:
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
答案 1 :(得分:0)
您必须覆盖onOptionsItemSelected,因为您正在尝试使用操作栏的后退按钮。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
然后覆盖BackPressed -
@Override
public void onBackPressed()
{
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
}
else {
super.onBackPressed();
}
}