如何检测我是哪个碎片?

时间:2014-05-27 03:43:46

标签: android android-fragments android-actionbar-compat

只是一个问题,我正在使用片段在android中创建一个应用程序,当它打开时会加载一个列表视图,当我触摸一个元素时它转到下一个片段并向我显示内容,我启用了{{ 1}}获取操作栏上的后退按钮,但我不知道如何返回ListView片段,我是这样做的

bar.setDisplayHomeAsUpEnabled(true);

但它让这个动画再次打开窗口,它工作但看起来很讨厌,我也想在ListView片段中禁用按钮,因为在应用程序之外没有其他地方可以去,所以我想要在ListView片段中隐藏它,类似于:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) 
       {        
          case android.R.id.home:  
              Intent intent = new Intent(getApplicationContext(), MainActivity.class);
              startActivity(intent);
              finish();
          default:            
             return super.onOptionsItemSelected(item);    
       }
}

此外我正在使用它来制作Action Bar,因为我需要支持2.3+设备:

if(this.equals(MainActivity.class)){
//Hide the back button in action bar
}else{
//display ir
}

2 个答案:

答案 0 :(得分:0)

您需要做的是使用FragmentTransactionFragmentTransaction.addToBackStack()添加到Backstack。然后当您按后退或向上按钮时,将加载上一个Fragment

答案 1 :(得分:0)

你的方式是打开一个Activity,而不是Fragment,而Fragment有一个方法就是“替换”,你可以用它来选择你想要的Fragment! 并且您可以使用FragmentManager的findFragmentById()来查找片段

enter image description here