Theo
Theo

Reputation: 3149

Handling Rotation in Android

I run a webservice get the data I want and display them in another activity. So far everything is running smoothly. And now ROTATION!:). This is what I am doing. I use the onSaveInstanceState(...) method to save the values I want in key/values pairs. After that I use the onRestoreInstanceState(...) method to read the stored values. This is my code.

public class Extras extends AppCompatActivity {
TextView textView;
TextView articleTextView;
String image;
String title;
String article;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clubnews_extra);

    textView = (TextView)findViewById(R.id.textView2);
    articleTextView = (TextView)findViewById(R.id.articleTextView);

    imageView = (ImageView)findViewById(R.id.imageNews);

    Intent i = getIntent();

    title = i.getStringExtra("title");
    article = i.getStringExtra("article");
    image = i.getStringExtra("image");

    textView.setText(Html.fromHtml(title));
    articleTextView.setText(Html.fromHtml(article));

    Picasso.with(this).load(image).into(imageView);



}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);

    outState.putString("title",title);
    outState.putString("article",article);
    outState.putString("image",image);
}

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

    savedInstanceState.getString("title");
    savedInstanceState.getString("article");
    savedInstanceState.getString("image");
 }
}

When I am putting breakpoint inside the onSaveInstanceState I see nothing when I rotate the phone/emulator. That makes me thing that the values are not stored.

Please advice.

Thanks.

Upvotes: 0

Views: 28

Answers (1)

Faiz Malkani
Faiz Malkani

Reputation: 313

You're overriding the wrong method.

Use

public void onSaveInstanceState(Bundle outState)

Instead of,

public void onSaveInstanceState(Bundle outState, 
                            PersistableBundle outPersistentState)

Upvotes: 1

Related Questions