Android Second Activity TextView临时历史记录丢失与覆盖问题
解决你的Android历史记录丢失&覆盖问题
嘿,兄弟,我看了你的代码,马上就明白问题出在哪了——你每次打开Historial Activity都是在创建一个全新的实例,之前的历史数据根本没地方存,而且你把TextView设成static的做法其实还藏着内存泄漏的风险,咱们一步步来改。
问题根源拆解
- 你现在每次调用
PasarHistorial(),都会启动一个新的Historial Activity,之前打开过的那个已经被销毁或者处于后台,新实例里的TextView全是空白的,自然只会显示当前传的那一组数据,之前的记录就没了。 - 返回MainActivity再重新打开Historial时,又是一个全新的实例,所有控件都重新初始化,历史记录当然丢失。
下面给你两个简单易上手的解决方案,都是符合你「临时存储、关闭App就丢」的需求:
方案一:用全局列表存历史记录(新手友好,直观易懂)
这个思路是把所有历史记录存在MainActivity的一个列表里,每次打开Historial时把整个列表传过去,一次性渲染所有记录。
步骤1:修改MainActivity的代码
在MainActivity里添加一个列表来保存历史,然后修改PasarHistorial()方法:
// 在MainActivity类里声明全局列表 private List<String> historyList = new ArrayList<>(); public void PasarHistorial() { // 把当前结果拼成字符串加入历史列表 String historyItem = "Resultado :" + counter1 + " : " + counter2; historyList.add(historyItem); // 把整个列表传递给Historial Intent myIntent = new Intent(MainActivity.this, Historial.class); // 因为ArrayList实现了Serializable,所以可以直接传递 myIntent.putStringArrayListExtra("HISTORY_LIST", new ArrayList<>(historyList)); startActivity(myIntent); }
步骤2:重构Historial Activity的代码
去掉static的TextView(千万别再这么用了!),改成成员变量,然后在onCreate里接收列表并渲染:
public class Historial extends AppCompatActivity { private TextView H1, H2, H3, H4, H5, H6, H7, H8, H9; private Button Regresar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_historial); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 初始化所有控件 initViews(); // 获取传递过来的历史列表 ArrayList<String> historyList = getIntent().getStringArrayListExtra("HISTORY_LIST"); if (historyList != null) { // 遍历列表,把每条记录设置到对应的TextView里 for (int i = 0; i < historyList.size() && i < 9; i++) { switch (i) { case 0: H1.setText(historyList.get(i)); break; case 1: H2.setText(historyList.get(i)); break; case 2: H3.setText(historyList.get(i)); break; case 3: H4.setText(historyList.get(i)); break; case 4: H5.setText(historyList.get(i)); break; case 5: H6.setText(historyList.get(i)); break; case 6: H7.setText(historyList.get(i)); break; case 7: H8.setText(historyList.get(i)); break; case 8: H9.setText(historyList.get(i)); break; } } } } // 抽离初始化控件的方法,代码更整洁 private void initViews() { H1 = findViewById(R.id.Historia1); H2 = findViewById(R.id.Historia2); H3 = findViewById(R.id.Historia3); H4 = findViewById(R.id.Historia4); H5 = findViewById(R.id.Historia5); H6 = findViewById(R.id.Historia6); H7 = findViewById(R.id.Historia7); H8 = findViewById(R.id.Historia8); H9 = findViewById(R.id.Historia9); Regresar = findViewById(R.id.Regresar); Regresar.setOnClickListener(v -> finish()); } }
方案二:让Historial保持单实例运行(更高效)
这个思路是让Historial Activity始终只有一个实例,每次打开时不会重建,而是复用已有的实例,这样就能保留之前的TextView内容。
步骤1:修改AndroidManifest.xml
给Historial Activity加上launchMode="singleTop":
<activity android:name=".Historial" android:launchMode="singleTop"/>
步骤2:修改MainActivity的PasarHistorial()方法
不用再传ContH了,只需要传当前的两个结果:
public void PasarHistorial() { Intent myIntent = new Intent(MainActivity.this, Historial.class); myIntent.putExtra("Res1", counter1); myIntent.putExtra("Res2", counter2); startActivity(myIntent); }
步骤3:重构Historial Activity的代码
添加onNewIntent()方法来接收新传递的数据,同时维护一个计数来记录当前已添加的历史条数:
public class Historial extends AppCompatActivity { private TextView H1, H2, H3, H4, H5, H6, H7, H8, H9; private Button Regresar; private int currentHistoryCount = 0; // 记录当前已添加的历史条数 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_historial); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); initViews(); // 处理第一次打开时传递的数据 handleNewHistory(getIntent()); } // 当Activity已经在栈顶时,会调用这个方法接收新的Intent @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleNewHistory(intent); } private void initViews() { H1 = findViewById(R.id.Historia1); H2 = findViewById(R.id.Historia2); H3 = findViewById(R.id.Historia3); H4 = findViewById(R.id.Historia4); H5 = findViewById(R.id.Historia5); H6 = findViewById(R.id.Historia6); H7 = findViewById(R.id.Historia7); H8 = findViewById(R.id.Historia8); H9 = findViewById(R.id.Historia9); Regresar = findViewById(R.id.Regresar); Regresar.setOnClickListener(v -> finish()); } private void handleNewHistory(Intent intent) { if (intent == null) return; int res1 = intent.getIntExtra("Res1", 0); int res2 = intent.getIntExtra("Res2", 0); String historyText = "Resultado :" + res1 + " : " + res2; // 如果还没到9条记录,就添加到对应的TextView if (currentHistoryCount < 9) { switch (currentHistoryCount) { case 0: H1.setText(historyText); break; case 1: H2.setText(historyText); break; case 2: H3.setText(historyText); break; case 3: H4.setText(historyText); break; case 4: H5.setText(historyText); break; case 5: H6.setText(historyText); break; case 6: H7.setText(historyText); break; case 7: H8.setText(historyText); break; case 8: H9.setText(historyText); break; } currentHistoryCount++; } // 如果超过9条,可以加个提示或者覆盖最早的记录,这里先简单处理 } }
给新手的小提醒
- 绝对不要把UI控件(比如TextView)设为static,这会导致Activity销毁后控件还被引用,造成内存泄漏,时间长了App会崩溃。
- 方案一更适合新手理解,方案二更高效,你可以根据自己的需求选。
- 两种方案都满足「临时存储、关闭App就丢失」的要求,完美符合你的需求。
内容的提问来源于stack exchange,提问作者Maximiliano Sincovich
相关产品推荐
相关产品推荐

