Android恢复活动变量消失了

Posted

技术标签:

【中文标题】Android恢复活动变量消失了【英文标题】:Android resume activity variables gone 【发布时间】:2021-09-08 04:10:51 【问题描述】:

短: 我有三个班级:A (MainActivity), B (Secondary), C(Third).AB 的父级是 C. 在A 中,我在B 上添加了额外的int idForUsageBidForUsage 存储在变量 int chosenId 中(工作正常)。B 执行 Stuff 并使用 Extra int chosenIdint secondIdForUsage 生成 Intent(也可以正常工作)。@987654334 @ 做的东西,它工作得很好。

当我现在单击左上角的小“后退按钮”以访问父活动时,应用程序崩溃了,因为我试图访问变量 chosenId,它似乎被设置为默认值 -1 (即使我想再次阅读 Extra。)

public class MainActivity extends AppCompatActivity 

    //references to Buttons etc
    ...

    public static final String ChosenID = "com.example.Abzeichenschwimmer.ChosenSwimmerID";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //set up button stuff
        ...
        //ListView which has clickable Items which trigger the Activity
        lv_swimmerList = findViewById(R.id.lv_schwimmerListe);

        
        //Listeners
        
        lv_swimmerList.setOnItemClickListener(this::onListViewItemClick);
        
    @Override
    protected void onResume()
        super.onResume();
        updateSchwimmerliste(dataBaseHelper);
    
    public void onListViewItemClick(AdapterView<?> parent, View view, int position, long id) 
                SchwimmerModel clickedSchwimmer = (SchwimmerModel) parent.getItemAtPosition(position);

                Intent intent = new Intent(MainActivity.this, DisplaySchwimmer.class);
                //Toast.makeText(MainActivity.this, String.valueOf(clickedSchwimmer.getId()), Toast.LENGTH_SHORT).show();
                intent.putExtra(ChosenSwimmerID, clickedSchwimmer.getId());
                startActivity(intent);

    
    

public class DisplaySchwimmer extends AppCompatActivity 
    int chosenSwimmerID;
    
    public static final String SchwimmerID = "com.example.Abzeichenschwimmer.schwimmerID";
    public static final String AufgabenID = "com.example.Abzeichenschwimmer.aufgabenID";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_schwimmer);
        
        lv_exc = findViewById(R.id.lv_aufgaben);

        
        refreshValues();
        showAufgabenOnListView(dataBaseHelper);
        lv_exc.setOnItemClickListener(this::onListViewItemClick);
    
    public void getIntentExtra()
        Intent intent = getIntent();
        chosenSwimmerID = intent.getIntExtra(MainActivity.ChosenSwimmerID,-1);
    
    public void onDeleteClick(View view)
        SchwimmerModel toDeleteSwimmer = (SchwimmerModel) dataBaseHelper.getSchwimmerByID(chosenSwimmerID);
        dataBaseHelper.deleteSchwimmer(toDeleteSwimmer);
        Toast.makeText(this, "deleted", Toast.LENGTH_SHORT).show();
        DisplaySchwimmer.this.finish();
    
    public void refreshValues()
        getIntentExtra();
        SchwimmerModel schwimmer = dataBaseHelper.getSchwimmerByID(chosenSwimmerID); <--- Main Error
        
    
    
    private void showAufgabenOnListView(DataBaseHelper dataBaseHelper) 
        getIntentExtra();
        ArrayAdapter<ExcerciseModel> schwimmerArrayAdapter = new ArrayAdapter<ExcerciseModel>(DisplaySchwimmer.this, android.R.layout.simple_list_item_1, dataBaseHelper.getExcersisesForSwimmerByID(chosenSwimmerID));
        lv_exc.setAdapter(schwimmerArrayAdapter);
    

    public void onListViewItemClick(AdapterView<?> parent, View view, int position, long id) 
        ExcerciseModel clickedExcerciseModel = (ExcerciseModel) parent.getItemAtPosition(position);

        Intent intent2 = new Intent(DisplaySchwimmer.this, DisplayAufgabe.class);
        intent2.putExtra(SchwimmerID, chosenSwimmerID);
        intent2.putExtra(AufgabenID, clickedExcerciseModel.getId());
        Log.e("aaa", String.valueOf(chosenSwimmerID));
        startActivity(intent2); <-- Intentstart
    
    @Override
    protected void onResume()
        super.onResume();
        showAufgabenOnListView(dataBaseHelper);
    


我希望代码(删除了很​​多行)可以作为概述。也许有人知道解决方案。 谢谢马克西姆斯

【问题讨论】:

【参考方案1】:

当您从DisplayAufgabe 回按到DisplaySchwimmer 时(意图始终为空)

因为您调用 getIntent di DisplaySchwimmer,您将获得默认值 -1(额外的空意图)

当您尝试调用 dataBaseHelper.getSchwimmerByID(chosenSwimmerID); 时,意味着您尝试在数据库上获取索引 -1。因为访问索引-1,你总是会出错。

我的建议

在调用 dbHelper 之前添加验证,即

if (chosenSwimmerID > -1)
   SchwimmerModel schwimmer = dataBaseHelper.getSchwimmerByID(chosenSwimmerID);

只有当值可用时才 getExtra

if (intent.hasExtra(MainActivity.ChosenSwimmerID))
   chosenSwimmerID = intent.getIntExtra(MainActivity.ChosenSwimmerID,-1);

【讨论】:

【参考方案2】:

这一切都归结为使用 sharedPreferences。这有很大帮助。 Second post from me 更简单地解释了这个问题,我找到了解决方案。

【讨论】:

以上是关于Android恢复活动变量消失了的主要内容,如果未能解决你的问题,请参考以下文章

回到应用程序时,Android 后台活动消失

Android阻止恢复活动

Android:恢复 singleTask 活动

Android - 恢复上次查看的活动

使用完成按钮单击关闭 android 活动(完全,甚至不在后台)

恢复android中活动的动态UI元素