简体   繁体   中英

Android: Result of TextView.setText() overridden by fragment

I'm a bit new to using fragments, and am having an issue with setting the text of a TextView within a fragment.

Right now, I have a single Activity with a common set of six buttons, along with two fragments which are displayed in a LinearLayout. I am able to use the buttons to replace the fragment in the LinearLayout successfully. However, I'm noticing some strange behavior related to changing a TextView in those two fragments.

I have a method in the fragments' class, called setTimer(), which attempts to change the text in the TextView. The strange thing is, the method works successfully. However, a split second later, the text reverts back to the default text in the TextView contained in the fragment's layout .xml file (which is a blank string).

I've tried calling the setTimer() method both before and after I replace the fragment in the LinearLayout, and the results are the same either way. How can I change this TextView's contents without having them overridden moments later? Thank you!

MainActivity.java's onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        FocusFragment initialFragment = new FocusFragment();
        initialFragment.setFragmentType( Constants.FRAGMENT_WORK );
        initialFragment.setTimer( 25 );
        getFragmentManager().beginTransaction()
                .add( R.id.linearLayout_fragmentHolder, initialFragment, "fragment_work" )
                .commit();
    }
}

FocusFragment.java:

public class FocusFragment extends Fragment {

    int fragment_type;
    static TextView timer;

    final String TAG = "FocusFragment";

    public FocusFragment() {

    }

    public void setFragmentType( int fragment_type ) {
        this.fragment_type = fragment_type;
    }

    public int getFragmentType() {
        return this.fragment_type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View rootView;
        if ( getFragmentType() == Constants.FRAGMENT_WORK ) {
            rootView = inflater.inflate( R.layout.fragment_work, container, false );
        } else {
            rootView = inflater.inflate( R.layout.fragment_break, container, false );
        }
        timer = ( TextView ) rootView.findViewById( R.id.textView_timer );
        return rootView;
    }

    public boolean setTimer( int timerValue ) {
        if ( timer != null ) {
            if ( timerValue < 10 ) {
                timer.setText( "0" + timerValue + ":00" );
            } else {
                timer.setText( timerValue + ":00" );
            }
            Log.d( TAG, "Timer text set successfully." );
            return true;
        }
        Log.w( TAG, "WARNING: setTimer() couldn't find the timer TextView!" );
        return false;
    }
}

Finally, the changeFragment() method, which is called when the buttons are pressed:

public void changeFragment( String fragmentName, int timerValue ){
    FocusFragment fragment = new FocusFragment();
    if ( fragmentName == "fragment_work" ) {
        fragment.setFragmentType( Constants.FRAGMENT_WORK );
    } else {
        fragment.setFragmentType( Constants.FRAGMENT_BREAK );
    }
    fragment.setTimer( timerValue );
    getFragmentManager().beginTransaction()
            .replace( R.id.linearLayout_fragmentHolder, fragment, fragmentName )
            .commit();
}

The problem is that the OnCreateView() method of the fragment is called after the setTimer() is called.

An easy way to solve this is to first call fragment.setTimerValue(value) when you create the fragment.

void setTimerValue(int value){
    this.timerValue = value;
}

Then at the end of OnCreateView() method do:

OnCreateView(){
    ...
    setTimer(timerValue);
    return rootView;
}

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