Best practices for avoiding activity chaining and subsequent multpile instances of activities?

StackOverflow https://stackoverflow.com/questions/5343905

  •  27-10-2019
  •  | 
  •  

Question

I've got the following menu on all 4 of my activities

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
            case R.id.aboutme:
                android.content.Intent i = new android.content.Intent(this, About.class);
                startActivity(i);
                return true;
            case R.id.help:
//                showHelp();
                return true;
            case R.id.languageselection:
                android.content.Intent i2 = new android.content.Intent(this, com.jameselsey.LanguageSelection.class);
                startActivity(i2);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

This means that from any of my activities I can hit the menu button and go to the About me page, or the Help page etc.

However, each time I do that I'm effectively creating a new instance of that activity and loading it up, as if I keep clicking back I retrace all of the previous activities I've had open.

Are there any resource implications of this? Is there any way that I can navigate away from one activity and kill the one I've moved away from?

Was it helpful?

Solution

You can call finish() on the activity, you're going away from.

OTHER TIPS

You can call finish() after startActivity(i) and it should kill the activity that you are leaving.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top