0

Ceci est mon onbackpressed que j'ai sur la page principale. Comment puis-je gérer ce défi sur mon projet.C'est que sur un backpress à la page principale sur double arrêt de l'application ??? J'ai mis à jour le fichier java comment l'utilisateur peut-il cliquer sur un backpress du fragment à cette maison et sur le double pour arrêter l'application ??Comment avoir onbackpress sur le tiroir de navigation (fragments)

import android.content.Intent; 
    import android.os.Bundle; 
    import android.support.design.widget.FloatingActionButton; 
    import android.support.design.widget.NavigationView; 
    import android.support.design.widget.Snackbar; 
    import android.support.v4.view.GravityCompat; 
    import android.support.v4.widget.DrawerLayout; 
    import android.support.v7.app.ActionBarDrawerToggle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.app.Fragment; 
    import android.app.FragmentTransaction; 
    import android.widget.Toast; 

    public class Menu extends AppCompatActivity 
      implements NavigationView.OnNavigationItemSelectedListener { 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_menu); 
      android.app.Fragment fragment = new MenuFragment(); // create a fragement object 
      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      ft.replace(R.id.mainFrame, fragment); 
      ft.commit(); 


      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
      fab.setOnClickListener(new View.OnClickListener() { 

    //this the email icon on the home page 
       @Override 
       public void onClick(View view) { 
        String myEmail[]={"[email protected]"}; 
        Intent sendMail = new Intent(Intent.ACTION_SEND); 
        sendMail.putExtra(Intent.EXTRA_EMAIL,myEmail); 
        sendMail.putExtra(Intent.EXTRA_SUBJECT,"(Type your subject)"); 
        sendMail.setType("plain/text"); 
        //incase you have to add something else put here 
        sendMail.putExtra(Intent.EXTRA_TEXT,"Your phone:+2547"); 
        startActivity(sendMail); 


       } 

      }); 

      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
      drawer.setDrawerListener(toggle); 
      toggle.syncState(); 

      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
      navigationView.setNavigationItemSelectedListener(this); 
     } 
    //taxi module 
     public void taxi_id(View view){ 
      Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.Taxi.class); 
      startActivity(timer);} 
    //host home module 
     public void hosthome_id(View view){ 
      Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.MainActivity.class); 
      startActivity(timer);} 
     //calling enterprenuers module 
     public void enter_id(View view){ 
      Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.SectionListView.class); 
      startActivity(timer);} 
     //calling UKV/ICV 
     public void ukvicv_id(View view){ 
      Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.UkvIcv_Home.class); 
      startActivity(timer);} 
     public void finacial_id(View view){ 
      Intent finacial=new Intent(this,sampleapp.razen.com.sampleapp.FinacialHomePage.class); 
      startActivity(finacial);} 



     @Override 
     public void onBackPressed() { 
      // TODO Auto-generated method stub 
      Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.Menu.class); 
      startActivity(timer); 

     } 





     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 

      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 

     public void displayView(int viewId) { 

      Fragment fragment = null; 
      String title = getString(R.string.app_name); 

      switch (viewId) { 
       case R.id.nav_12wks_program: 
        Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.ExpandableListMainActivity.class); 
        startActivity(timer); 

        break; 


       case R.id.nav_emergency_contact: 
        Intent flow=new Intent(this,sampleapp.razen.com.sampleapp.EmergencyContact.class); 
        startActivity(flow); 

        break; 


       case R.id.nav_survey: 
        fragment = new SurveyFragment(); 
        title = "Survey"; 
        break; 

       case R.id.nav_psld: 
        fragment = new PsldFragment(); 
        title = "Psld"; 

        break; 
       case R.id.nav_security: 
        fragment = new SecurityFragment(); 
        title = "All Volunteers Mu"; 

        break; 
       case R.id.nav_ukv: 
        fragment = new UkvKiswaFragment(); 
        title = "Ukv's kiswahili"; 

        break; 

       case R.id.nav_agreement: 
        fragment = new AgreementFragment(); 
        title = "Agreement "; 

        break; 
       case R.id.nav_njoro_geo: 
        fragment = new GeoNjoroFragment(); 
        title = "Njoro Geography "; 

        break; 




      } 

      if (fragment != null) { 
       FragmentTransaction ft = getFragmentManager().beginTransaction(); 
       ft.replace(R.id.mainFrame, fragment); 
       ft.commit(); 
      } 

      // set the toolbar title 
      if (getSupportActionBar() != null) { 
       getSupportActionBar().setTitle(title); 
      } 

      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      drawer.closeDrawer(GravityCompat.START); 

     } @SuppressWarnings("StatementWithEmptyBody") 

     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      displayView(item.getItemId()); 
      return true; 
     } 
    } 
+0

Pourquoi vous commencez une activité 'onBackPressed()'? –

+0

J'essayais juste d'un clic pour aller à la page principale –

+0

Si je vous ai bien compris, vous voulez fermer le tiroir de navigation en appuyant une seule fois sur le dos. Droite? –

Répondre

0

Changer votre onBackPressed() comme ci-dessous:

@Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

Vous devez ajouter les opérations de fragment à la backstack pour la navigation arrière correcte des fragments.

Modifier votre displayView(...) comme ci-dessous:

... 
.. 
if (fragment != null) { 
       FragmentTransaction ft = getFragmentManager().beginTransaction(); 
       ft.replace(R.id.mainFrame, fragment) 
       .addToBackStack(null);//Note this 
       ft.commit(); 
      } 
... 
.. 

aussi changer votre onCreate() comme ci-dessous:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_menu); 
    android.app.Fragment fragment = new MenuFragment(); // create a fragement object 
    FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.add(R.id.mainFrame, fragment);//Note this 
    ft.commit(); 
... 
.. 
} 
+0

je l'ai sur mon java principal mais sur ist cliquez sur il suffit d'arrêter l'application lorsque j'ai cliqué sur l'un des fragments –

+0

Je pense que vous êtes parler des fragments de contenu ne pas le tiroir de navigation. –

+0

ouais désolé pour cela –

0

Créer un mécanisme de minuterie (50-100 millisecondes minuterie. Vous pouvez utiliser la minuterie, les classes TimerTask) pour connaître les clics suivants de l'utilisateur sur le bouton arrière et définir une valeur de l'indicateur en fonction du cycle de vie clic et minuterie. Si l'utilisateur tape deux fois avant d'expirer la minuterie, ce qui signifie que vous devez quitter l'application, sinon la minuterie expire aller à la page principale.

+0

donnez-moi un lien d'où je peux trouver un lien –

0

intérieur onBackPressed s'il y a un fragment dans backstack que popBackStack() autrement tout simplement finish() votre activité.

Pour plus de détails ont un regard sur How to close one fragment when isVisible