大型Android活动写作会对我的应用程序造成任何影响

时间:2011-02-21 19:56:36

标签: android switch-statement android-intent

我正在制作我的第一个官方应用程序投放市场,我想知道如果我只使用它们来提取文本,那么120多个新活动会不会太多?如果是这样,我可以将MysecondActivity转换为巨大的if或switch语句吗? 这是我在一本书中找到的例子我改变了案例0:一旦我得到答案,我将改变其余部分类似于案例0。

提前感谢您,对于凌乱的代码和写作

感到抱歉
         AndroidManifest.xml

       <activity android:name=".MySecondActivity" />

        ListActivityExample.java
        import android.app.ListActivity;
        import android.app.SearchManager;
        import android.content.Intent;
        import android.net.Uri;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.ListView;
        import android.widget.AdapterView.OnItemClickListener;

    public class ListActivityExample extends ListActivity{
        static final String[] ACTIVITY_CHOICES = new String[] {
            "Open new Actvity",
            "Open Contacts",
            "Open Phone Dialer Example",
            "Search Google Example",
            "Start Voice Command"
        };
        final String searchTerms = "superman";

        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);

            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            getListView().setTextFilterEnabled(true);
            getListView().setOnItemClickListener(new OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3){
                switch(arg2) {
                 case 0: //opens new screen
                {Intent intent = new Intent(ListActivityExample.this,MySecondActivity.class);
                            startActivity(intent);
                        break;}
                    case 1: //opens phone dialer and fills in the given number
                        {
                        startActivity(new Intent(Intent.ACTION_VIEW,
                                Uri.parse("content://contacts/people/")));
                        break;}
                    case 2:            
                        {
                          startActivity(new Intent(Intent.ACTION_VIEW,
                                Uri.parse("tel:12125551212")));
                         break;}
                    case 3: //
                        {
                        Intent intent1= new Intent(Intent.ACTION_WEB_SEARCH);
                        intent1.putExtra(SearchManager.QUERY, searchTerms);
                        startActivity(intent1);
                        break;}
                    case 4: // 
                        {startActivity(new
                                        Intent(Intent.ACTION_VOICE_COMMAND));
                        break;}
                        default: break;
                    }
                }
            });
        }
        }


    MySecondActivity.java

    mport android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class MySecondActivity extends Activity {
         @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             TextView tv = new TextView(this);
             tv.setText("text here");
             setContentView(tv);
             }
    }

1 个答案:

答案 0 :(得分:1)

我无法看到这个代码会导致大型Activity堆栈出现问题。只要它们不是同时堆叠在一起,就可以根据需要启动任意数量的活动。使用此代码,您可以通过点击列表中的项目来启动新的Activity,但是用户必须在返回列表时完成新的Activity(按回)以启动其他内容,因此它们不会全部在内存中马上。

这有帮助吗?或者我误解了你的问题?