2016-05-08 5 views
-1

私はサインアッププロセスのために5つのフラグメントを配置しています。最初のフラグメントには2つの編集テキストと1つのテキストビューがあります。編集テキストは姓と名を表し、テキストビューは次のフラグメントに移動します2。私がfrag2からfrag1に戻ると、そのfirstnameとlastnameの値は消えます。 onSavedInstanceメソッドにputStringメソッドを使用して値を格納しようとしましたが、onCreateメソッドで値を取得しましたが、それは役に立ちませんでした。代わりに戻り中にフラグメントのEditTextsに値を保持する

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

のここでのコードがある...

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); 
} 
+0

:とあなたのonCreate内のコードをコメントアウトして、onCreateViewの内側にトップラインを交換してみてください? – varunkr

+0

フレグ2の戻るボタンです。 –

+0

あなたは私の答えを試しましたか? – varunkr

答えて

0

使用

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

編集

これが機能していないなら、あなたは使用する必要がありますあなたのトランザクションでaddToBackstack、それは確実に動作します。ただし、メインアクティビティではonBackPressedの機能を上書きし、以前のものに戻る前にフラグメントをポップするpopbackStackも必要です。 Thisは、それを上書きする必要があります。

+0

これまでにaddメソッドを使用していましたが、同じものを置き換えるために着いた... –

+0

@AalapPatelは編集を見ています – varunkr

0

onCreateの中に入っているsavedInstanceデータを上書きしていて、設定している値がonCreateViewになっているようです。 [戻る]ボタンを経由して、戻ってfragment1に来るのかどのように

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 
関連する問題