0

I've got an issue where setting visibility in my onResumeFragments method doesn't seem to work. Here's the Activity in question:

public class MainActivity extends FragmentActivity {

    private ViewGroup activityBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activityBar = (ViewGroup)findViewById(R.id.activity_bar);
    }

    @Override
    protected void onResumeFragments() {
        if (someCondition) {
            activityBar.setVisibility(View.GONE);
        }
    }
}

The activity bar looks like this:

<LinearLayout
    android:id="@+id/activity_bar"
    android:layout_height="@dimen/activity_bar_height"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:background="#AD000000"
    android:layout_alignParentTop="true"
    android:gravity="center_vertical"
    android:visibility="visible"
    >

I've taken out lots of other details, but basically, I want to hide this activity bar in my onResumeFragments if a given condition is met. What I've found is that the setVisibility doesn't seem to do anything. I can call getVisiblity on the activityBar after that line is called and see it set to GONE, but it's still shows up in the UI. I can also set it's Y and see it move down the screen. I've also double checked to make sure this is happening in the Main thread. I'm sort of at a loss for why this isn't working and what the best workaround is.

Morinar
  • 3,460
  • 8
  • 40
  • 58
  • LinearLayout activityBar; activityBar = (LinearLayout)findViewById(R.id.activity_bar); // give it a try and let us know if it works. – danny117 Oct 07 '14 at 17:51
  • I was pretty sure that wouldn't make a difference since `LinearLayout` extends `ViewGroup` but I tried it anyway. Same result. – Morinar Oct 07 '14 at 18:40
  • Thanks for trying. Yes Views are pretty much every gui element. – danny117 Oct 07 '14 at 19:41

2 Answers2

0

While dealing with onStart(), and onResume(), things can get a little tricky. For example, you should not commit transactions inside the onResume() method of FragmentActivity, as there are some cases in which the method can be called before the activity's state has been restored (see the documentation for more information). If your application requires committing a transaction in an Activity lifecycle method other than onCreate(), do it in either FragmentActivity#onResumeFragments() or Activity#onPostResume(). So just try to modify your code and see whether this works in onPostResume().

  • As mentioned in my question itself, it doesn't work in `onResumeFragments()`. I just tried it in `onPostResume()` and it doesn't seem to work there either. – Morinar Oct 07 '14 at 16:29
0

It turned out that the activity_bar was being animated before I was trying to set it's visibility. The information contained in this ticket was the same issue that I was having and had the same solution.

Why doesn't setVisibility work after a view is animated?

Community
  • 1
  • 1
Morinar
  • 3,460
  • 8
  • 40
  • 58