2017-03-17 211 views
3

我有導航抽屜與片段,與5菜單,問題是當我去菜單4,我按後退按鈕應用程序關閉,但我需要的應用程序回到第一個菜單,這是所有的菜單分段。如何檢測當在按鈕片斷android按下後按鈕?

這是主要活動(抽屜式導航)代碼

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(); 




} 

菜單的這一個(片段)

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

在片段中創建一個函數,並使用片段對象從onBackpressed()從活動中調用它。 –

回答

2

在您的MainActivity

@Override 
public void onBackPressed() 
{ 
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.containerView,new Recommendation()).commit(); 
} 
+0

感謝它的工作:)) –

3

在主要活動中,您需要覆蓋以下功能

@Override 
public void onBackPressed() 
{ 
    // code here to check what fragment you are on and handle that accordingly 
    super.onBackPressed(); // this exits the app. 
} 

文檔可以在這裏找到:https://developer.android.com/reference/android/app/Activity.html#onBackPressed()

+1

***「代碼..檢查你在哪個片段上」*** - 請參閱'getActiveFragment()'這裏:https://stackoverflow.com/a/15543580/2162226 – gnB

0

您可以在Android的按鍵監聽使用setOnKeyListener()。然後,如果你要處理的背襯,覆蓋onKey方法:

@Override 
public boolean onKey(View v, int keyCode, KeyEvent event) { 
    if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { 
     // do your thing 
    } 

} 
+1

後退按鈕不會發送一個關鍵事件,它調用onBackPressed代替。 –

+0

@GabeSechan:國際海事組織,它的工作原理。我已經在Dialog Fragment中測試過了,所以它也應該在fragment中工作。 – Yashasvi

+1

它不起作用。它自2010年以來一直無法使用。後退按鈕和主屏幕按鈕無法通過onKeyPressed進行捕捉。如果你找到了一個它所在的地方,它可能是來自OEM的一個bug或者一個奇怪的操作系統版本。 –

-1

片段你應該使用這樣的事情:

@Override 
    //Pressed return button - returns to the results menu 
    public void onResume() { 
     super.onResume(); 
     getView().setFocusableInTouchMode(true); 
     getView().requestFocus(); 
     getView().setOnKeyListener(new View.OnKeyListener() { 
      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 

       if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){ 

       //your code 

        return true; 
       } 
       return false; 
      } 
     }); 
    }