Ich füge zwei Fragmente in meiner Aktivität, Fragment1 enthält einen Button und fragment2 enthält eine TextView, wenn ich auf die Schaltfläche in fragment1 es Zähler erhöht und Ergebnisse in TextView von fragment2 zeigt. Ich bin mit dem saveInstanceState und speichern Sie den Zähler und den Text im Falle der Drehung des Bildschirms. Aber immer noch Fehler ist da .............. Fehler ist, wenn ich Anwendung ausführen, es funktioniert gut, aber sobald ich den Bildschirm drehen ist es Onclick-Methode überhaupt nicht aufrufen. Ich stecke hier fest, bitte hilf mir.onClickListener funktioniert nicht richtig in Fragment
public class FragmentA extends Fragment{
Button button;
int counter;
Communicator communicator;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a,container,false);
button = (Button) view.findViewById(R.id.button);
if(savedInstanceState == null)
counter = 0;
else{
counter = savedInstanceState.getInt("Counter",0);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
communicator.respond("Button was clicked "+counter+ " times");
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("Counter",counter);
}
public void setCommunicator(Communicator communicator){
this.communicator = communicator;
}
interface Communicator{
public void respond(String data);
}
}
Ich habe beide Optionen versucht, den Knopf mit Hörer in onCreateView Bindung() und onActivityCreate(), um das Problem besteht.
public class FragmentB extends Fragment{
TextView textView;
String data;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
if (savedInstanceState != null) {
textView = (TextView) view.findViewById(R.id.textView);
data = savedInstanceState.getString("text");
textView.setText(data);
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView = (TextView) getActivity().findViewById(R.id.textView);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("text", data);
}
public void changeData(String data){
this.data = data;
textView.setText(data);
}
}
dies die Aktivität ist, wo ich diese Fragmente bin mit ....
public class MainActivity extends AppCompatActivity implements FragmentA.Communicator{
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
FragmentA fragmentA;
FragmentB fragmentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentA = new FragmentA();
fragmentB = new FragmentB();
fragmentTransaction.add(R.id.fragment_container1,fragmentA,"Najam");
fragmentTransaction.add(R.id.fragment_container2,fragmentB,"Najam");
fragmentTransaction.commit();
fragmentA.setCommunicator(this);
}
@Override
public void respond(String data) {
//set the text of fragment_B
fragmentB.changeData(data);
}
}
Hier sind die einfachen XML-Dateien
Dies ist für das Fragment A
Diese<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFB0">
<Button
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me"
android:id="@+id/button" />
</LinearLayout>
ist für fragmentB
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAA333">
<TextView
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="you clicked the button 0 times"
android:id="@+id/textView" />
</LinearLayout>
Dies ist die Aktivität
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFBB00"
android:id="@+id/main_layout"
tools:context="com.najam.fragments1.MainActivity">
<FrameLayout android:id="@+id/fragment_container1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout android:id="@+id/fragment_container2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Mögliche Duplikat [Methode fordert android Bildschirmdrehung] (http://stackoverflow.com/questions/36112869/method-calls-on-android-screen-rotation) – karan
Wie initialisieren Sie die 'Kommunikator '? Kann den Code nicht finden, der es tut. – Egor
Dank @ Karan, aber ich habe Angst, dass die Lösung nicht der Fall ist. – Najam