3

I read almost all article about onSaveInstanceState and onRestoreInstanceState in Stack overflow but I cant solve my problem.
I have a text view and button in my main.java and while you click on button value of a ( a is an int variable) variable will increase and show in the text view, but, when I rotate my phone ( orientation change), text view reset.

I override onSaveInstanceState and onRestoreInstanceState but it doesn't work. one more thing, I have 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 {
     /** Called when the activity is first created. */   
   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 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 on this. I really appreciate your help.

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
Reza Shek
  • 581
  • 1
  • 8
  • 18
  • 3
    seriously? this is probably the worst formatted code i have ever seen... – Alex Lockwood Apr 21 '12 at 14:35
  • @AlexLockwood I know I was going to `edit` and tidy up but then I was like W...T...H – Blundell Apr 21 '12 at 14:41
  • @Alexlockwood :D. u r right because I'm a fresh man in android. – Reza Shek Apr 21 '12 at 14:58
  • this is quite helpful http://developer.android.com/training/basics/activity-lifecycle/recreating.html – Syed Raza Mehdi Jul 14 '15 at 07:00
  • Does this answer your question? [Save state of activity when orientation changes android](https://stackoverflow.com/questions/13022677/save-state-of-activity-when-orientation-changes-android) – A P Apr 27 '20 at 13:18
  • seems like a duplicate of https://stackoverflow.com/questions/13022677/save-state-of-activity-when-orientation-changes-android/61459807#61459807 – A P Apr 27 '20 at 13:18

5 Answers5

7

As an update, you could just set freezesText="true"

http://developer.android.com/reference/android/R.attr.html#freezesText

 <TextView
    android:id="@+id/eltemas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:freezesText="true"/>

Or


This is the simplest example:

TextView yourTextView;

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

    yourTextView = (TextView) findViewById(R.id.your_textview);

}

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("YourTextViewTextIdentifier", yourTextView.getText().toString());

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        yourTextView.setText(savedInstanceState.getString("YourTextViewTextIdentifier"));
    }

But having an inner class that represents your state will be much nicer when you start saving a lot of objects on orientation change.

Using an inner class it would look like this below. You can imagine as you have more and more state to save the inner class makes it much easier (less messy) to handler.

  private static class State implements Serializable {

    private static final String STATE = "com.your.package.classname.STATE";

    private String yourTextViewText;

    public State(String yourTextViewText) {
        this.yourTextViewText = yourTextViewText;
    }

    public String getYourTextViewText() {
        return yourTextViewText;
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    State s = new State(yourTextView.getText().toString());

    outState.putSerializable(State.STATE, s);

    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    State s = (State) savedInstanceState.getSerializable(State.STATE);

    yourTextView.setText(s.getYourTextViewText());
}
Blundell
  • 75,855
  • 30
  • 208
  • 233
5

I think this code is helpful for you..

public class MainActivity extends Activity 
{

    protected void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);
        System.out.println("TAG, onSavedInstanceState");

        final TextView text = (TextView)findViewById(R.id.textView1);
        CharSequence userText = text.getText();
        outState.putCharSequence("savedText", userText);
    }
    protected void onRestoreInstanceState(Bundle savedState) 
    {       
        System.out.println("TAG, onRestoreInstanceState");
        final TextView text = (TextView)findViewById(R.id.textView1);
        CharSequence userText = savedState.getCharSequence("savedText");
        text.setText(userText);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String name = "5";

        final TextView show = (TextView)findViewById(R.id.textView1);
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() 
        {

            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                show.setText(name);

            }
        });
    }
}
Luca
  • 823
  • 4
  • 11
  • 31
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
  • If we have different text color when updating the textview text. how can we save that too? can you help me with this? – Gurjit Singh Aug 31 '20 at 09:04
0

you need to reconstruct your view components with saved values. fill your textview with the saved value.

@Override   
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  a = savedInstanceState.getInt ("salavat-count");
  tv.setText(""+a);
}
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48
0

A neglected but serious mistake. You have to first pass the data to onSavedInstanceState() and the call onSavedInstanceState() . If you think then you will find that its useless to call onSavedInstanceState before passing the data to be saved. it wont save the activity instance as onSavedInstanceState ()is called before.. It should be as :

@Override
protected void onSaveInstanceState(Bundle SavedInstanceState) 
{           
    SavedInstanceState.putInt("salavat-count", a);
    super.onSaveInstanceState(SavedInstanceState); 
}
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
DUDE_MXP
  • 724
  • 7
  • 24
-1

Do you really want to use these methods ?

Here you go a kind of solution to your problem, just put this inside your manifest file

<activity android:name="package.subpackage.xptoactivity" android:configChanges="orientation"></activity>

If you do this, your Activity won't reload all the views reseting their values when you change the orientation

Leandro Silva
  • 804
  • 1
  • 9
  • 28
  • 4
    This is not a solution, it's a workaround to orientation restarts. Your activity can be restarted during a number of events, changing orientation is only one of them. – srgtuszy Sep 28 '13 at 23:25
  • It is widely and publicly advised to avoid this practice, as it has stopped being even a workaround as android has evolved. – Omaraf Jul 23 '15 at 03:31