【问题标题】:Retain values in EditTexts of a fragment while returning返回时保留片段的 EditTexts 中的值
【发布时间】:2016-05-08 03:31:38
【问题描述】:

我有一个活动,我为注册过程放置了 5 个片段。第一个片段有两个编辑文本和一个文本视图。编辑文本用于名字和姓氏,文本视图将我带到下一个片段 2。当我从 frag2 回到 frag 1 时,那些名字和姓氏值消失了。我尝试使用 putString 方法在 onSavedInstance 方法中存储值并在 onCreate 方法中检索它,但它没有帮助。这是代码...

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState==null){

    }
    else{
        firstName=savedInstanceState.getString("firstname");
        lastName=savedInstanceState.getString("lastname");
        fname.setText(firstName);
        lname.setText(lastName);
    }
}

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.frag1,container,false);
    fname = (EditText) rootView.findViewById(R.id.si_firstname);
    lname = (EditText) rootView.findViewById(R.id.si_lastname);
    firstName = fname.getText().toString();
    lastName = lname.getText().toString();
    next1 = (TextView) rootView.findViewById(R.id.next1);    


    next1.setOnClickListener(new View.OnClickListener() { //takes to frag2.
        @Override
        public void onClick(View v) {

            if(firstName.isEmpty() || lastName.isEmpty()){
                Toast.makeText(getActivity().getApplicationContext(), "no name", Toast.LENGTH_LONG).show();
            }
            else {

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, secondFragment);
                fragmentTransaction.commit();
            }
        }
    });

    return rootView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putString("firstname", firstName);
    outState.putString("lastname", lastName);
}

【问题讨论】:

  • 你如何通过返回按钮回到 fragment1?
  • 是 frag 2 上的后退按钮...
  • 你试过我的答案了吗?

标签: android android-fragments


【解决方案1】:

代替

fragmentTransaction.replace(R.id.fragment_container, secondFragment);

使用

fragmentTransaction.add(R.id.fragment_container, secondFragment);

编辑

如果这不起作用,那么您将不得不在您的事务中使用addToBackstack,这肯定会起作用。但是,您还需要在 Main 活动和 popbackStack 中覆盖 onBackPressed 函数以弹出片段,然后再返回到前一个片段。 This 是您需要覆盖它的方式。

【讨论】:

    【解决方案2】:

    您似乎正在用您在onCreateView 中设置的值覆盖您在onCreate 中获取的savedInstance 数据。尝试注释掉 onCreate 中的代码,并将 onCreateView 中的顶行替换为:

    View rootView = inflater.inflate(R.layout.frag1,container,false);
    fname = (EditText) rootView.findViewById(R.id.si_firstname);
    lname = (EditText) rootView.findViewById(R.id.si_lastname);
    next1 = (TextView) rootView.findViewById(R.id.next1);
    
    if(savedInstanceState != null){ //the data's on the savedinstance
       firstName = savedInstanceState.getString("firstname");
       lastName = savedInstanceState.getString("lastname");
       //restore the editTexts
       fname.setText(firstName);
       lname.setText(lastName);
    }else{
       firstName = fname.getText().toString();
       lastName = lname.getText().toString();
    
       //whatever you want to do on the first run
       //or if the data's not on the savedInstance
    }
    
    //whatever else you want to do on every run
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多