1

Android,继承DialogFragment重写了onCreateView方法。在onCreateView方法中初始化了一些View的数据。当再次通过show(FragmentManager, String)显示时Dialog中View中的数据是上次手动设置的数据,在onCreateView方法中初始化数据的代码好像运行了却没有效果。

是不是我的写法有问题,还是因为在Android 4.2和4.4中对话框自动保存之前输入的内容的特性?如果是后者,该怎么不让它自动保存?
++++++++++++++++++++++++++++++++++++
更新:
想到了生命周期后,上网查了一下,调试后发现在onViewStateRestored(Bundle savedInstanceState)方法中放入设置监听方法和设置初始值的代码比较好。不过系统还是会在onActivityCreated(Bundle savedInstanceState)onViewStateRestored(Bundle savedInstanceState)之间初始化上次手动设置的数据。这种行为在某些情况下很让人不爽啊。有谁知道怎么不让它发生?或者有更好的方法,感谢告知。
++++++++++++++++++++
增加代码:

package sept.test;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
    private TestDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        dialog = new TestDialog();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            dialog.show(getFragmentManager(), "TestDialog");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
package sept.test;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
 * Created by TrapaQuad on 2015.12.22.
 */
public class TestDialog extends DialogFragment
        implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
    private static final String TAG = "TestDialog";
    private LinearLayout llRoot;
    private CheckBox mCheckBox;
    private EditText mEditText;
    private Boolean mChecked;

    @Override
    public void onAttach(Activity activity) {
        Log.e(TAG, "onAttach(Activity)");
        super.onAttach(activity);
    }
    @Override
    public void onAttach(Context context) {
        Log.e(TAG, "onAttach(Context)");
        super.onAttach(context);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onCreate");
        super.onCreate(savedInstanceState);
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Log.e(TAG, "onCreateDialog");
        return super.onCreateDialog(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView");
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        llRoot = (LinearLayout)inflater.inflate(R.layout.dialog_test, container);
        return llRoot;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Log.e(TAG, "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.e(TAG, "onActivityCreated");
        super.onActivityCreated(savedInstanceState);

        mCheckBox = (CheckBox)llRoot.findViewById(R.id.checkBox);
        mEditText = (EditText)llRoot.findViewById(R.id.editText);
        mCheckBox.setOnCheckedChangeListener(this);


        Log.e(TAG, "-----onActivityCreated--------:"+ mChecked+"|"+mEditText.getText()+"|");
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        Log.e(TAG, "-----onViewStateRestored--------:"+ mChecked+"|"+mEditText.getText()+"|");
        Log.e(TAG, "onViewStateRestored");
        Log.e(TAG, "-------------:"+ mChecked+mEditText.getText());
        mCheckBox.setChecked(true);
        mEditText.setText("QSSB");
        Log.e(TAG, "-sdsdsdsdsdsds------------:"+ mChecked+mEditText.getText());
        super.onViewStateRestored(savedInstanceState);
    }
    @Override
    public void onStart() {
        Log.e(TAG, "onStart");
        Log.e(TAG, "-------------:"+ mChecked);
        super.onStart();
    }
    @Override
    public void onResume() {
        Log.e(TAG, "onResume");
        Log.e(TAG, "-------------:"+ mChecked);
        super.onResume();
        Log.e(TAG, "-------------:" + mChecked);
    }
    @Override
    public void onCancel(DialogInterface dialog) {
        Log.e(TAG, "onCancel");
        mEditText.setText("onCancel");
        super.onCancel(dialog);
    }
    @Override
    public void onDismiss(DialogInterface dialog) {
        Log.e(TAG, "onDismiss");
        mEditText.setText("onDismiss");
        super.onDismiss(dialog);
    }
    @Override
    public void onPause() {
        Log.e(TAG, "onPause");
        mEditText.setText("onPause");
        super.onPause();
    }
    @Override
    public void onStop() {
        Log.e(TAG, "onStop");
        mEditText.setText("onStop");
        super.onStop();
        mEditText.setText("onStop2");
    }
    @Override
    public void onDestroyView() {
        Log.e(TAG, "onDestroyView");
        mEditText.setText("onDestroyView");
        super.onDestroyView();
    }
    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        mEditText.setText("onDestroy");
        super.onDestroy();
    }
    @Override
    public void onDetach() {
        Log.e(TAG, "onDetach");
        mEditText.setText("onDetach");
        super.onDetach();
        Log.e(TAG, "-------------:" + mChecked+mEditText.getText());
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.e(TAG, "onSaveInstanceState");
        super.onSaveInstanceState(outState);
    }
    @Override
    public void onClick(View v) {

    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.e(TAG, "onCheckedChanged");
        mChecked = isChecked;
    }
}
2015-12-23 提问
2 个回答
0

调用show方法只是把之前创建的DialogFragment对象推到栈顶,而不是重新创建一个DialogFragment对象,不会重走onCreateView方法,之前对象持有的数据当然还保存着。想要不保存数据内容,重新new一个DialogFragment对象啊

0

解决思路总结:
1.在DialogFragment中重写onViewStateRestored()方法,设置不缓存数据,如题主示例那样。
2.在View中,如果是TextView 可以在组件中设置android:freezesText="false" ,如果是其他组件如EditText设置此属性,无效。可以自定义组件并重写getFreezesText() 返回false即可。

撰写答案

推广链接