0

Forgive my English :(

I have a problem with "NavigationDrawer". I have the Fragments "Home " that have no special activities, "Import, Gallery and SlideShow", all with activities running perfectly. However, if I click on Import (HOME> IMPORT) the activity IMPORT opens, but if I click on (IMPORT> GALLERY / SLIDESHOW / HOME) the Import activity remains open. I have to press the back button to go to HOME, to click on another activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    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) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

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

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    //here is the main place where we need to work on.
int id=item.getItemId();
    switch (id){

        case R.id.nav_home:
            Intent h= new Intent(Home.this,Home.class);
            startActivity(h);
            break;
        case R.id.nav_import:
            Intent i= new Intent(Home.this,Import.class);
            startActivity(i);
            break;
        case R.id.nav_gallery:
            Intent g= new Intent(Home.this,Gallery.class);
            startActivity(g);
            break;

        case R.id.nav_slideshow:
            Intent s= new Intent(Home.this,Slideshow.class);
            startActivity(s);
            break;
        // oh nightmare
    }



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

IMPORT ACTIVITY

public class Import extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_import);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Button Button9 = (Button) findViewById(R.id.Button9);
    Button Button11 = (Button) findViewById(R.id.Button11);

    //We dont need this.


    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) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Button9.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent it = new Intent(Import.this, PHP5.class);
            startActivity(it);
        }
    });

    Button11.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent it = new Intent(Import.this, PHP7.class);
            startActivity(it);
        }
    });


}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

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

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    //here is the main place where we need to work on.
    int id=item.getItemId();
    switch (id){

        case R.id.nav_home:
            Intent h= new Intent(Import.this,Home.class);
            startActivity(h);
            break;
        case R.id.nav_import:
            Intent i= new Intent(Import.this,Import.class);
            startActivity(i);
            break;
        case R.id.nav_gallery:
            Intent g= new Intent(Import.this,Gallery.class);
            startActivity(g);
            break;
        case R.id.nav_slideshow:
            Intent s= new Intent(Import.this,Slideshow.class);
            startActivity(s);
            break;

        // after this lets start copying the above.
        // FOLLOW MEEEEE>>>
        //copy this now.
    }



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

}

2 Answers2

0

Maybe you forgot to call finish() in the IMPORT Activity? If you call it after your Intent to another Activity, the current Activity will close, and your Home Activity will be shown again.

gi097
  • 7,313
  • 3
  • 27
  • 49
0

You need to remove all previous Activities from stack, it can be done trough a flag in the intent

         Intent it = new Intent(Import.this, OtherAct.class);
         it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
         startActivity(it);

Else a combination of the keywors may work for you:

         it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

Check the doc about how it works: https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • I try It but the problem persist – João Vitor GA Oct 25 '17 at 18:57
  • public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. //here is the main place where we need to work on. int id=item.getItemId(); switch (id){ case R.id.nav_home: Intent h= new Intent(Home.this,Home.class); h.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(h); break; – João Vitor GA Oct 25 '17 at 18:58
  • I used the code from this video https://www.youtube.com/watch?v=M_4Oh2FeRYs – João Vitor GA Oct 25 '17 at 19:06