FloatingActionButton 在 onResume 中调用 getVisibility 时可见,尽管它不应该

问题描述 投票:0回答:1

我有以下情况。

我有一个不可见的 FloatingActionButton。 当我通过最小化应用程序将其置于后台时,会调用 onStop。 当我把它带回来时,我的 onResume 方法突然说它是可见的,我不知道为什么。

这是我的方法:

@Override
protected void onStop() {
    super.onStop();
    int isVisible = fabStopRecording.getVisibility();
    if(isVisible==0) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("StopButtonIsVisible", true);
        this.getIntent().putExtras(bundle);
    }
}

@Override
protected void onResume() {
    super.onResume();
    loadSharedPreferences();
    //retrieving Bundle when returning
    if (this.getIntent().getExtras() != null) {
        Bundle bundle = this.getIntent().getExtras();
        boolean isStopButtonVisible = bundle.getBoolean("StopButtonIsVisible");
        if(isStopButtonVisible) {
            fabStopRecording.setVisibility(View.VISIBLE);
        }
    }
    //redraw Google Map, calling GoogleMap will fail due to NPE
    mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

在我的布局中,FloatingActionButton 设置为 android:visibility="invisible"。

也许有人可以告诉我我在这里做错了什么!

提前非常感谢您

android onresume
1个回答
0
投票

一如既往,我在寻求帮助后发现了问题:)

我也实现了 onSaveInstanceState 和 onRestoreInstanceState 方法,当将应用程序带入后台并再次将其带入前台时会调用它们。我不知道这一点。

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("StopButtonIsVisible", true);
    getIntent().putExtras(outState);
}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if(savedInstanceState != null) {
        boolean isStopButtonVisible = savedInstanceState.getBoolean("StopButtonIsVisible", false);
        if (isStopButtonVisible) {
            fabStopRecording.setVisibility(View.VISIBLE);
            fabPauseRecording.setVisibility(View.VISIBLE);
            fabStartRecording.setVisibility(View.INVISIBLE);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.