除非菜单活动和内容活动之间的布局和行为有显着差异,否则我会采用“仅几个活动方法”。
后退按钮不会损坏,Android 会跟踪您的积压。
您真的需要 89 种不同的布局吗?如果内容主要是它们之间的不同之处,请将内容存储在数据库或文件或/res/ 中,重用布局并在运行时填充布局的内容区域。
更新:
每个内容页面有 1 个活动不是最简单的吗?onCreate() 用适当的内容填充布局?
取决于您的onCreate() 代码会有多大的不同。如果您的onCreate() 将成为一个巨大的switch,并带有62 个截然不同的case 子句,那么最好进行单独的小型活动,而不是大型活动。如果您可以概括 onCreate() 代码,并将其保留在 100 行和 10 个分支语句以下,那将是可行的方法。
举个例子,前段时间我正在构建一个简单的应用程序,其中包含一系列考试问题,并将它们随机呈现给用户。每个问题都有问题文本、插图和 2-4 个答案选项。大约有 500 个不同的问题。这是从数据库加载问题并更新布局的代码。请注意,它处理可变数量的答案,以及一些没有插图的问题的可能性。
public void loadQuestion(int id) {
// Columns in questions table:
// CREATE TABLE questions (
// id integer,
// q text,
// a1 text,
// a2 text,
// a3 text,
// a4 text,
// a5 text,
// correct integer,
// img blob
// );
Cursor c = mDatabase.rawQuery("SELECT * FROM questions where id=" + id, null);
c.moveToFirst();
TextView text = (TextView) findViewById(R.id.text);
text.setText(c.getString(1));
RadioGroup g = (RadioGroup) findViewById(R.id.answers);
g.clearCheck();
// Load answers!
int correct = c.getInt(7);
int[] buttons = new int[] {R.id.a1, R.id.a2, R.id.a3, R.id.a4, R.id.a5};
for (int i=0; i < 5; i++) {
String answerText = c.getString(i + 2);
RadioButton rb = (RadioButton) findViewById(buttons[i]);
if (answerText != null && answerText.length() > 0) {
rb.setText(answerText);
rb.setTag(i + 1 == correct ? "correct" : null);
rb.setVisibility(RadioButton.VISIBLE);
} else {
rb.setVisibility(RadioButton.GONE);
}
}
byte[] encoded = c.getBlob(8);
ImageView iv = (ImageView) findViewById(R.id.image);
if (encoded != null && encoded.length > 0) {
iv.setVisibility(ImageView.VISIBLE);
iv.setImageBitmap(bytesToBitmap(encoded));
} else {
iv.setVisibility(ImageView.GONE);
}
}
您的意思是:返回按钮不会损坏,Android 会跟踪您的积压?
当用户从活动移动到
活动,跨应用程序,
Android系统保持线性
活动导航历史
用户访问过。这是活动
栈,也称为后栈。
一般来说,当用户开始一个新的
活动,它被添加到活动中
堆栈,以便按 BACK 显示
堆栈上的上一个活动。
(来自Activity and Task Design Guidelines)