简体   繁体   English

如何在Android中的“自定义视图”上保存实例状态?

[英]How to save instance state on Custom View in Android?

I have aa custom View that extends LinearLayout : 我有一个扩展LinearLayout自定义视图:

public class CustomEditTextLogin extends LinearLayout {
    public CustomEditTextLogin(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        initialize(context);
    }
    public CustomEditTextLogin(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initialize(context);
    }
    private LinearLayout llParentCetL;
    private TextView txtAlertCs;
    private EditText edt;
    private ImageView img;
    public static final int TEXT = 0;
    public static final int EMAIL = 1;
    public static final int PASSWORD = 2;
    public static final int USERNAME = 3;
    private void initialize(Context context) {
        LayoutInflater mLayoutInflater = (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mLayoutInflater.inflate(R.layout.custom_edit_text_login, this, true);
        llParentCetL = (LinearLayout) view.findViewById(R.id.llParentCetL);
        txtAlertCs = (TextView) view.findViewById(R.id.txtAlertCetL);
        edt = (EditText) view.findViewById(R.id.edtCetL);
        img = (ImageView) view.findViewById(R.id.imgCetL);

        txtAlertCs.setVisibility(View.GONE);
        int imgMargin = (int) (UIHelpers.width *0.025);
        UIHelpers.setMargin(img, imgMargin, imgMargin, imgMargin, imgMargin);
        img.setOnClickListener(new OnClickListener() {   
            @Override
            public void onClick(View view) {
                setFocus();
            }
        });
        public CustomEditTextLogin setFocus(){
            if(edt != null){
                edt.setFocusableInTouchMode(true);
                edt.requestFocus();

                InputMethodManager imm = (InputMethodManager) App.context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT);
            }
            return this;
        }
    }

I want to save instance state and restore it, but it is different from Activity's instance state. 我想保存实例状态并还原它,但这与Activity的实例状态不同。

How to save instance state on custom view in Android? 如何在Android的自定义视图上保存实例状态?

Not sure that I understood you correctly. 不确定我是否正确理解您。 But looks like you want to save the state your View independently from Activity instance and be able to restore it. 但是看起来您想独立于Activity实例保存View的状态并能够还原它。 You can use retained fragments for this. 您可以为此使用保留的片段。

  • Extend the Fragment class and declare references to your stateful objects. 扩展Fragment类并声明对有状态对象的引用。
  • Call setRetainInstance(boolean) when the fragment is created. 创建片段时调用setRetainInstance(boolean)。
  • Add the fragment to your activity. 将片段添加到您的活动中。
  • Use FragmentManager to retrieve the fragment when the activity is restarted. 重新启动活动时,使用FragmentManager检索片段。

     public class RetainedFragment extends Fragment { // data object we want to retain private MyDataObject data; // this method is only called once for this fragment @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // retain this fragment setRetainInstance(true); } public void setData(MyDataObject data) { this.data = data; } public MyDataObject getData() { return data; } 

    } }

To restore saved object you can use FragmentManager. 要恢复保存的对象,可以使用FragmentManager。

    public class MyActivity extends Activity {

    private RetainedFragment dataFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // find the retained fragment on activity restarts
        FragmentManager fm = getFragmentManager();
        dataFragment = (DataFragment) fm.findFragmentByTag(“data”);

        // create the fragment and data the first time
        if (dataFragment == null) {
            // add the fragment
            dataFragment = new DataFragment();
            fm.beginTransaction().add(dataFragment, “data”).commit();
            // load the data from the web
            dataFragment.setData(loadMyData());
        }

        // the data is available in dataFragment.getData()
        ...
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // store the data in the fragment
        dataFragment.setData(collectMyLoadedData());
    }
}

Here you can find more information about that: http://developer.android.com/guide/topics/resources/runtime-changes.html 在这里您可以找到有关此的更多信息: http : //developer.android.com/guide/topics/resources/runtime-changes.html

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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