简体   繁体   中英

When I press the back button of my phone the app closes

I have made an app with many activities and I have included a bottom navigation bar to all of them. I used finish(); after any intent not to get any memory leak problem but when I press the back button of my phone the app closes. What should I do?

//an  intent of my app 


        Button btnsuita = (Button) findViewById(R.id.souita_btn);
        btnsuita.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(HotelRooms.this, HotelRoomsSouita.class);
                startActivity(intent);
                finish();

            }
        });



  //An Example of Bottom Navigation Bar
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.BottomNavView_bar);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        Menu menu = bottomNavigationView.getMenu();
        android.view.MenuItem menuItem = menu.getItem(0);
        menuItem.setChecked(true);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {

                    case R.id.ic_home:
                        Intent intent1 = new Intent(HotelRoomsSouita.this, MainMenu.class);
                        startActivity(intent1);
                        finish();
                        break;

                    case R.id.ic_back:
                        Intent intent2 = new Intent(HotelRoomsSouita.this, HotelRooms.class);
                        startActivity(intent2);
                        finish();
                        break;

                    case R.id.ic_contact_us:
                        Intent intent3 = new Intent(HotelRoomsSouita.this, Mail.class);
                        startActivity(intent3);
                        finish();
                        break;
                }


                return false;
            }

        });

Remove finish() in the switch every case.

If you want to ignore back button clicks, then you can add this code in the activity.

@Override
public void onBackPressed() {
   //Do anything if you wish
}

Since you are finishing every activity ,there is o activity remain in the backstack or background . so the application closes.

You can use view pager to inflate fragment for bottom navigation view . This is the correct approach by google material design guid lines.

Never use activity for bottom navigation view .

And still if you continue with your activity , then override the onBackpress method of the activtiy .

If I understand your problem correctly you want the user to be able to go back in previous activities while your application does not cause any memory leaks(by starting same activity again and again)

Solution:

  • Remove finish() from each case in onNavigationItemSelected
  • Go to your manifest file and add android:launchMode="singleInstance" to each Activity
  • Ovveride onBackPressed so that it does nothing in your first-started activity as dcanh121 said.

If you do that then if user tries to open the same Activity which was already opened before, it will cause the app to open the previous activity instead of creating a new one.. ( read more about this )

Therefore you won't have any memory leak problems :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM