大型 android Activity 写入会对我的应用程序造成任何影响吗
我正在制作我的第一个正式应用程序并投放市场,我想知道如果我仅使用它们来提取文本,超过 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);
}
}
I'm making my first official application to put on the market and i was wondering would over 120 new activities be too many if I'm only using them to pull text? If so is there a way I could turn MysecondActivity to a giant if or switch statement?
This is the example I found in a book I changed up case 0: I'm going to change up the rest be similar to case 0 once i get an answer.
Thank you in advance and sorry for messy code and writing
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看不出这段代码会导致大型活动堆栈出现问题。启动任意数量的活动,只要它们不是同时堆叠在一起即可。使用此代码,您可以通过点击列表中的项目来启动新的活动,但用户在返回列表以启动其他内容时必须完成新的活动(按返回键),因此它们并不全部在内存中立刻。
这有帮助吗?或者我误解了你的问题?
There's nothing I can see about this code that will cause an issue with a large Activity stack. Start as many Activities as you like, as long as they aren't all stacked together at the same time. With this code you are starting a new Activity as a result of tapping an item in the list, but the user must finish the new Activity (press back) when returning to your list to start something else, so they aren't all in memory at once.
Does that help? Or did I misunderstand your question?