我有带片段的项目和导航抽屉,导航抽屉包含4个菜单: 1.建议 2.勒克斯
问题是当我在Lux菜单中然后我按下后退按钮它应该转到推荐(这是主菜单)。但当我在推荐和我按下后退按钮它应该关闭应用程序,如何检测我是否在推荐菜单?
主要活动
public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the DrawerLayout and NavigationView
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new Recommendation()).commit();
/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_lux_level_recomen) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new Recommendation()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_room_index_calc) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new RoomIndex()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_utilization_factor_calc) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new UtilizationFactorCalculator()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_conversions) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new Conversion()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_lux) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new LuxSensor()).commit();
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
Lux菜单
public class LuxSensor extends Fragment {
TextView textLIGHT_available, textLIGHT_reading;
FragmentManager mFragmentManager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.lux_sensor,null);
textLIGHT_reading
= (TextView)x.findViewById(R.id.LIGHT_reading);
SensorManager mySensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);
Sensor LightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if(LightSensor != null){
mySensorManager.registerListener(
LightSensorListener,
LightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}else{
textLIGHT_available.setText("Sensor.TYPE_LIGHT NOT Available");
}
FragmentManager fm = getFragmentManager();
return x;
}
private final SensorEventListener LightSensorListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_LIGHT){
textLIGHT_reading.setText(event.values[0]+" LUX");
}
}
};
答案 0 :(得分:0)
在您mCurrentFrag
中添加全局变量activity
,如下所示
private Fragment mCurrentFrag;
并在替换您的任何片段时,将其分配给该变量,例如:
if (menuItem.getItemId() == R.id.nav_item_lux_level_recomen) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
Fargment recommendation = new Recommendation()
fragmentTransaction.replace(R.id.containerView,recommendation).commit();
mCurrentFrag = recommendation;
}
并在onBackPressed()
中检查mCurrentFrag
if( mCurrentFrag instanceof Recommendation){
//close the app
}
答案 1 :(得分:0)
当你添加片段时,你将添加标签系统。" tag"将是片段的识别。
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.container_framelayout, fragment, tag);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
现在检查片段你可以编写代码:
Fragment test = (Fragment) getSupportFragmentManager().findFragmentById(R.id.container_framelayout);
String tag = test.getTag().toString();
从这个"标签"你可以理解现在打开哪个片段