What's the proper method to start a new Activity in Android?
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:
it开发者_如何转开发em.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:
- 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? WhilestartActivity
does just that. But then again, a new intent is created every time the item is selected, which leads me to the next question. - 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
- a) Set the intent once in the
- 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?
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.
精彩评论