2016-01-14 47 views
0

我有兩個片段A和B和接口溝通的A到B,片段A有一個按鈕和溝通與B改變文本,但每當方向改變按鈕失去它的聽衆 她是代碼旋轉後丟失它的監聽器

MainActivtiy

public class MainActivity extends AppCompatActivity implements ClicktoChange{ 
    FragmentB fragmentB; 
    FragmentA fragmentA; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     fragmentB = new FragmentB(); 
     fragmentA = new FragmentA(); 
     FragmentTransaction fr = getFragmentManager().beginTransaction(); 
     fr.add(R.id.container2,fragmentA); 
     fr.add(R.id.container,fragmentB); 
     fr.addToBackStack(null); 
     fr.commit(); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d("im","in Resume"); 
     FragmentTransaction fr = getFragmentManager().beginTransaction(); 
     fr.replace(R.id.container2, fragmentA); 
     fr.replace(R.id.container, fragmentB); 
     fr.addToBackStack(null); 
     fr.commit(); 

    } 

    @Override 
    public void changeTheText(String str) { 
     Log.d("we are in main clicked ", "yay"); 

     fragmentB.changeText(str); 
     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.container, fragmentB); 
     fragmentTransaction.addToBackStack(null); 
     fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 

     fragmentTransaction.commit(); 


    } 
} 

這裏是片段A

的代碼3210

B片段

public class FragmentB extends Fragment { 

    TextView Text; 
    String str; 
    public FragmentB() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     Log.d("frag b","Yes I'v been created"); 
    View view = inflater.inflate(R.layout.fragment_fragment_b, container, false); 
     if(savedInstanceState!=null){ 
      str = savedInstanceState.getString("text"); 
      Text = (TextView) view.findViewById(R.id.textfrag); 
      Text.setText(str); 
     } 
     return view; 
    } 


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     Text = (TextView) getActivity().findViewById(R.id.textfrag); 
     Log.d("the text is ", (Text == null) + ""); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
    outState.putString("text",str); 
    } 

    public void changeText(String str){ 
     this.str = str; 
     Text.setText(str); 
    } 

} 

終於接口

public interface ClicktoChange { 
    void changeTheText(String str); 
} 

回答

0

當你旋轉手機,該活動被破壞並重新啓動。你有兩個選擇。

1)告訴手機不要這樣做,購買添加一個configChanges行到您的清單。

2)檢測到發生了這種情況(當它發生時,你會在onCreate中得到一個非空的Bundle)並重新創建並附加偵聽器。

+0

你可以在片段A中看到Activity創建的方法 我記錄了Log.d(「yay」,「interface init」); 這個方法在調用後也被調用,並且監聽器附着在按鈕上,但它工作,所以我在這裏使用第二個選項,但它不工作,我不知道爲什麼 –

+0

也添加了'android:configChanges =「keyboardHidden | orientation」'但沒有任何工作,當我旋轉按鈕失去它的監聽器 –