开发者

How do I properly go about building an Android App?

Specifically, what kind of conventions in terms of Activities does one follow? If I'm building a program with many screens, do I create an Activity for each screen?

If I want to properly navigate between Activities, do I stick intents in every activity? I want to make this code as clean and efficient as 开发者_JAVA百科possible


In short: yes.

Although you can work around this by dynamically altering your UI inside a single Activity, android recommends that each application 'activity' should be coded in a separate Activity class.

See this quite good article on the android recommended way.

This Intent/Activity design pattern has many advantages, one of it being that you can override and extend other application activities with your own with matching intent filters.

I see that you are concerned about efficiency. Be assured that the Activity switching overhead is highly optimized in android (for example, a Dalvik instance is always preallocated, ready to handle a new activity without the context switching overhead).


The short answer: It really depends on how you want to lay out your app.

For example, if you want to have tabs, you can use a tabhost, which will easily switch between Activities for you.

If you want to launch Activities yourself, you can launch Activities with intents (as you mentioned in your question). An example is launching intents from a Button or ListView. For a ListView (with an OnItemClickListener) you might have something like:

(your ListView).setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int index,
    long arg3) {
        Intent intent = new Intent(TheActivityYou'reLaunchingFrom.this, OtherActivityYouWishToLaunch.class);
        startActivity(intent);
    }
}

The links I've provided have really good examples. When you wish to end the activity you launched from another activity, you can call finish(), which should be called from a different event (like clicking on a Button).

Also keep in mind that you can launch Activities with hopes of receiving data from the launched activity via startActivityForResult, which uses Bundles.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜