【问题标题】:Will big android Activity wiriting cause any effect to my App大型android Activity wiriting会对我的应用程序造成任何影响
【发布时间】:2011-02-21 19:56:35
【问题描述】:

我正在制作我的第一个正式应用程序以投放市场,我想知道如果我只是使用它们来提取文本,那么 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);
             }
    }

【问题讨论】:

    标签: android switch-statement android-intent


    【解决方案1】:

    我看不到这段代码会导致大型 Activity 堆栈出现问题。开始尽可能多的活动,只要它们不是同时堆叠在一起。使用此代码,您将通过点击列表中的一个项目来启动一个新的 Activity,但是用户必须在返回到您的列表以启动其他内容时完成新的 Activity(按返回),因此它们并不都在内存中一次。

    这有帮助吗?还是我误解了你的问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多