旋转设备时,片段状态不会保存

时间:2013-12-23 16:47:42

标签: java android eclipse android-fragments android-lifecycle

请查看以下代码

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class InputFragment extends Fragment {


    private SeekBar daysAsCustomerSeek;

    private View view;

    //Following variables will save the application state and load back
    //on resume

    private final String DAYS_AS_CUSTOMER_VALUE_HOLDER = "days as customer value";




    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //Intializing instance variables
        view = inflater.inflate(R.layout.input, container,false);



        daysAsCustomerSeek = (SeekBar)view.findViewById(R.id.days_as_customer_seekbar);

        //Set default max values
        daysAsCustomerSeek.setMax(210);
        Log.d("MY_TAG", "Re Created");
        return view;

    }

    //This method will save the instances
    @Override
    public void onSaveInstanceState(Bundle savedInstanceStateBundle)
    {
          super.onSaveInstanceState(savedInstanceStateBundle);

        savedInstanceStateBundle.putInt(DAYS_AS_CUSTOMER_VALUE_HOLDER, daysAsCustomerSeek.getProgress());

Log.d("SAVE_LOG", "Instances saved");
    }

    //This method will restore the instances
    @Override 
    public void onActivityCreated(Bundle savedInstanceStateBundle)
    {
        super.onActivityCreated(savedInstanceStateBundle);

        if(savedInstanceStateBundle!=null)
        {

            daysAsCustomerSeek.setProgress(savedInstanceStateBundle.getInt(DAYS_AS_CUSTOMER_VALUE_HOLDER));

        }
        Log.d("LOAD_LOG", "Instances loaded");
    }

}

当手机的方向发生变化时(当某人旋转设备时),此代码不会恢复保存状态。但是按下后退按钮时会这样做。我发现为什么Logs会发生这种情况。

12-23 16:41:36.623: D/SAVE_LOG(13078): Instances saved
12-23 16:41:45.332: D/MY_TAG(13078): Re Created
12-23 16:41:45.332: D/LOAD_LOG(13078): Instances loaded
12-23 16:41:45.352: D/LOAD_LOG(13078): Instances loaded
12-23 16:41:45.842: D/MY_TAG(13078): Re Created

如您所见,它将onCreateView()称为最后一种方法。因此,onActivityCreated()中完成的所有操作都已撤消。我尝试通过在onCreateView()末尾添加恢复代码来解决这个问题,但是Bundle NULL是{{1}}。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

覆盖onSaveInstanceState方法以保存您要保存的进度条或其他小部件的状态,并在onViewCreated方法中检索它们

答案 1 :(得分:0)

您需要覆盖onSaveInstanceState(Bundle savedInstanceState)并将要更改的应用程序状态值写入Bundle参数,如下所示:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
   savedInstanceStateBundle.putInt(DAYS_AS_CUSTOMER_VALUE_HOLDER, daysAsCustomerSeek.getProgress());
}

Bundle本质上是一种存储NVP ("Name-Value Pair")地图的方式,它将被传递到onCreate和onActivityCreated(),你可以在这里提取这样的值:

 @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
    daysAsCustomerSeek.setProgress(savedInstanceStateBundle.getInt(DAYS_AS_CUSTOMER_VALUE_HOLDER));

}

您通常会使用此技术为您的应用程序存储实例值(selectionsunsaved text等。)