I read almost the entire article on onSaveInstanceState and onRestoreInstanceState in stack overflow, but I cannot solve my problem.
I have a text view and a button in my main.java, and when you click on the value of the variable button ( a is an int variable), it will increase and appear in the text view, but when I rotate my phone (change orientation), the text view is reset .
I override onSaveInstanceState and onRestoreInstanceState, but this does not work. one more thing, i have a special layout-land.xml file for landscape view.
here is my code
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class main extends Activity {
public int a = 0;
public String fonts="TAHOMA.TTF";
TextView tv = (TextView) findViewById(R.id.salavat);
Button b = (Button) findViewById(R.id.showsalavat);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
a++;
tv.setText(""+a);
}
});
}
@Override
protected void onSaveInstanceState(Bundle SavedInstanceState) {
super.onSaveInstanceState(SavedInstanceState);
SavedInstanceState.putInt("salavat-count", a);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
a= savedInstanceState.getInt ("salavat-count");
}
}
and here is my main.xml
<TextView
android:id="@+id/shoma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/shoma"
android:textSize="20sp" />
<Button
android:id="@+id/showsalavat"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:text="+"
android:textSize="100sp" />
<TextView
android:id="@+id/eltemas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="55dp"
android:text="@string/eltemas" />
<TextView
android:id="@+id/salavat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp" />
<TextView
android:id="@+id/ferestade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="@string/salavat"
android:textSize="20sp" />
</LinearLayout>
I really need some help. I really appreciate your help.