Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:03:51+00:00 2026-05-25T19:03:51+00:00

While developing my first Android app I’ve come up with 3 different ways to

  • 0

While developing my first Android app I’ve come up with 3 different ways to start a new activity (there’ probably more that I’m unaware of) and I can’t understand if they are different or if they basically work in the same way.

For instance, I have a MainActivity with a menu (it’s an ActionBar component but works just like a menu). The menu has an option to open the AboutActivity.

My first approach was this:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);

        getMenuInflater().inflate(R.menu.actionbar_main, actionBar.asMenu());
        actionBar.findAction(R.id.actionbar_item_home).setIntent(new Intent(this, AboutActivity.class));
    }
}

My second approach, to simplify code organization, I started handling all menu item selections in their own method like this:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);

        getMenuInflater().inflate(R.menu.actionbar_main, actionBar.asMenu());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.actionbar_item_home:
                item.setIntent(new Intent(this, AboutActivity.class));
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

The third approach is very similar to the previous one, the only different is in the code line that starts the AboutActivity. Instead of:

item.setIntent(new Intent(this, AboutActivity.class));

I changed to:

startActivity(new Intent(this, AboutActivity.class));

But this got me thinking on the proper way to handle this. My questions:

  1. Between the approach #2 and #3, are there any differences between them? The way I see it, setIntent is basically defining the menu item Intent every time the item is selected. Which I’m assuming is a bad thing to do? While startActivity does just that. But then again, a new intent is created every time the item is selected, which leads me to the next question.
  2. On approach #1 I don’t have that issue, the intent is only created once in the onCreate method of the activity (as long as the activity is not destroyed I believe). But performance/memory wise (or anything else which might be relevant), are there any differences between the following?
    • a) Set the intent once in the onCreate method
    • b) Start the activity in the onOptionsItemSelected menu handler
  3. Now, assume a text field is on the AboutActivity. I press the menu item and open the activity, type anything in the text field, go back, press the menu item again and the text is gone. I though that creating the intent only once (approach #1) that the activity state would persist and the text field would be filled. But it doesn’t, it just acts exactly like all all other approaches, which confuses me even more. What are the differences between them all?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:03:52+00:00Added an answer on May 25, 2026 at 7:03 pm

    2 is wasteful, since like you said it’s creating that intent every time it’s called. It’s not terribly wasteful since these methods are not being called much, but it’s not a best practice.

    If you wanted to do something like option 1 but in a better location, onCreateOptionsMenu() would be the best choice.

    The “menus” are a special case for starting an activity. Normally you do something like #3 where you just call startActivity whenever you need it. The setIntent for menu’s just does that behind the scenes.

    I also think there’s a way to do this via XML. But I’m having trouble finding out for sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While developing my app I have come to realize that the majority of my
I'm currently developing my first android app, and my first game. I've been developing
While developing a Launcher (Homescreen) application for Android, I've come into a security exception
While developing a new query at work I wrote it and profiled it in
I've been experiencing very inconsistent results while developing an iPhone app and trying to
I am new to UserControls, and while developing my own control I found a
I am currently developing my first Android application by reading Dev Documentation at Android
I'm running into a crash issue while developing an iPhone app with Core Data.
I'm currently working on an app that works with Twitter, but while developing/testing (especially
While developing a C++ application, I had to use a third-party library which produced

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.