简体   繁体   中英

Fragments: Button pressed state not being restored when pressing BACK

I have a fragment embedded in a layout. This fragment has a very simple view (basically just a styled button with its pressed state being programmatically set).

When I start a new activity from the activity hosting the fragment, and press BACK to go back to the original activity the button is still disabled, but is now showing its unpressed state.

layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        class="com.mypackage.NavigationDrawerFragment"
        android:gravity="bottom"
        android:padding="5dp" />>

</RelativeLayout>

fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="horizontal"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="10dp" >

    <Button
            android:id="@+id/nav_home"
            style="@style/navigation_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/button_home"
            android:text="HOME"
            android:textColor="@drawable/button_text" />

</RelativeLayout>

In the fragment class, I simply do the following:

  • Disable the button
  • Set the pressed state of the button to true (so the downstate of the drawable is shown)

My fragment class:

public class NavigationDrawerFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        mDrawer = inflater.inflate(R.layout.fragment_navigation_drawer,
            container, false);
        mButtonHome = (Button) mDrawer.findViewById(R.id.nav_home);
        mButtonHome.setEnabled(false);
        mButtonHome.setPressed(true);
    }
}

Any idea why the enabled state of the button is preserved, but the pressed state is not?

As a work around for this, I am now only disabling the button and have added set_enabled="false" states to my drawables and colors. This state is identical to the "pressed" state.

drawable/button_home.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_home_down" android:state_enabled="false"/> <!-- disabled state -->
    <item android:drawable="@drawable/btn_home_down" android:state_pressed="true"/> <!-- pressed -->
    <item android:drawable="@drawable/btn_home" /> <!-- default -->
</selector>

drawable/button_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="@color/button_down" android:state_enabled="false"/><!-- disabled state -->
    <item android:color="@color/button_down" android:state_pressed="true"/> <!-- pressed -->
    <item android:color="@color/button_up" /> <!-- default -->
</selector>

This basically has the same effect as calling setEnabled(false) and setPressed(true) on a button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM