1

I have project with fragment and with navigation drawer, the navigation drawer contain 4 menu : 1. Recommendation 2. Lux

The problem is when i in Lux menu then i pressed back button it should go to Recommendation(Which is the main menu). But when i in Recommendation and i pressed back button it should close the app, how to detect if i were in Recommendation menu?

Main Activity

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 Menu

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");
        }
    }

};
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Josh Parinussa
  • 633
  • 2
  • 11
  • 28
  • I'm sure you would have look around solutions. You can find solution [here](http://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager) and [here](http://stackoverflow.com/questions/18375288/fragment-lifecycle-which-method-is-called-upon-show-hide). – Geek Mar 23 '17 at 06:02

2 Answers2

0

Add a global variable mCurrentFrag in you activity like this

private Fragment mCurrentFrag;

and When you are replacing any fragment of yours, assign it to that variable, for example:

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;
}

and in onBackPressed() check for mCurrentFrag

if( mCurrentFrag instanceof Recommendation){
    //close the app
}
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0

When u add the fragment the u will add the tag system."tag" will be identification for fragment .

    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();
    transaction.replace(R.id.container_framelayout, fragment, tag);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.commit();
    getSupportFragmentManager().executePendingTransactions();

Now for check the fragment u can write the code:

 Fragment test = (Fragment) getSupportFragmentManager().findFragmentById(R.id.container_framelayout);
 String tag = test.getTag().toString();

From this "tag" u can understand which Fragment now open

Prosanto
  • 914
  • 3
  • 15
  • 27