1

I have the following android layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="at.althuber.billy.MainListActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main_list_recyclereview" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="160dp"
    android:layout_marginRight="@dimen/fab_margin"
    android:src="@android:drawable/ic_menu_edit"
    android:visibility="invisible"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="90dp"
    android:layout_marginRight="@dimen/fab_margin"
    android:src="@android:drawable/ic_menu_camera"
    android:visibility="invisible"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:visibility="invisible"
    app:srcCompat="@drawable/plus" />

</android.support.design.widget.CoordinatorLayout>

fab is invisible on startup and becomes visible when all data is loaded. This works perfectly, whether started in landscape or in portrait mode. But when the device changes orientation, setVisibility() isn't working anymore.

Any Ideas why?

alat
  • 21
  • 1

3 Answers3

0

Sounds to me like you are calling your setVisibility in the wrong place. Are you loading data onCreate or loading data onResume. Then are you setting visibility after dataLoad complete callback? Have you confirmed that the callback gets called on rotating of device. Are you handling your rotation manually or letting Android lifecycle recreate your activity from scratch.

At any rate, you just need to move your setVisible code to on load complete of your data retrieving routine and move your data load routine to a life cycle callback that will be hit on rotation.

Sam
  • 5,342
  • 1
  • 23
  • 39
  • I let the Android lifecycle recreate my activity, so the code flow is the same when rotation occurs. I've also verifiied that my setVisibility is getting called. – alat Sep 29 '17 at 21:57
  • Then unfortunately you are going to have to share your activity code for help to spot the issue as something doesn't add up and guessing at it, won't help if we can't see what is happening in the code. Every developer leaves out small details to avoid complicating the question, but sometimes those details are the problem. – Sam Oct 02 '17 at 15:36
0

In Android, when orientation changes your activity will be destroyed AND re-created as usual that's means that the activity runs onCreate() -> onStart() -> onResume() -> activity running (Activity lifecycle). In this case, you should check and set visibility for a view is the same for both cases(when you start an activity at the first time or when orientation changes in the onCreate(), or onResume() method -> it's up to you).

So from API level 12, there is another way to keep an activity which isn't destroyed when orientation changes. See here this answer: Android, how to not destroy the activity when I rotate the device?

You should read carefully to understand before doing because it is complex.

Hope it helps

John Le
  • 1,116
  • 9
  • 12
0

It was my mistake. I had an Listener in a retained fragment, which i did not reattach when the activity is recreated during rotation. So the actions for the listener were executed in the old activity.

alat
  • 21
  • 1