kst
kst

Reputation: 1518

View was cleared when screen was rotated in Android

I have a View that was created at runtime then I draw some canvas on that View (runtime), after that I rotate my screen. All data was gone (reset). So I put the some code in AndroidManifest.xml like this

android:configChanges="keyboardHidden|orientation"

in my <activity> then I put an @Override function

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);      
  setContentView(R.layout.main);
  LinearLayout layout = (LinearLayout) findViewById(R.id.myPaint);
  layout.addView(mView); 
}

But this didn't solve my problem. I want to keep my data from View(runtime) on every single rotation.

Here's my onCreate function:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mView = new MyView(this);
    setContentView(mView);

    mView.requestFocus();
    setContentView(R.layout.main);
    LinearLayout layout = (LinearLayout) findViewById(R.id.myPaint);
    layout.addView(mView);
}

Upvotes: 1

Views: 5026

Answers (3)

paul miron
paul miron

Reputation: 1

I've just used my data-class as singleton (java-pattern). And it works fine. --> Application is a Stop-Timer for Racing, where i can stop time from different opponents on the track, so i need the data for longer time, also if the view is repainted.

regz

public class Drivers {
// this is my singleton data-class for timing

private static Drivers instance = null;

public static Drivers getInstance() {
        if (instance == null) {
            instance = new Drivers();
        }
        return instance;
    }
    // some Timer-Definitions.......
}

Then in MainActivity:

    // now the class is static, and will alive during application is running
    private Drivers drivers = Drivers.getInstance();

    @Override
    public void onClick(View v) {
        if (v == runButton1) {
           drivers.startTimer1();
            // do some other crazy stuff ...
        }
    }

    // here i put out the current timing every second
    private myUpdateFunction(){
        time01.setText(drivers.getTimer1());
        // update other timers etc ...
    }

Upvotes: 0

Greg Giacovelli
Greg Giacovelli

Reputation: 10184

You could approach this a few ways.

I assume MyView is your own class which extends View. If so there are two methods which you may care to know, onSaveInstanceState() and onRestoreInstanceState(). When saving you create a parcelable that will contain enough data for you to re-render your view if it were to be destroyed and recreated.

class MyView extends View {
   private String mString;

   onDraw(Canvas v) { ... }

   Parcelable onSaveInstanceState() {
    Bundle b = new Bundle();
    bundle.putString("STRING", mString);
    return b;


   void onRestoreInstanceState(Parcelable c) {
    Bundle b = (Bundle) c;
    mString = bundle.getString("STRING", null);

   }
}

Activity has similar state saving mechanics allowed in onCreate and onSaveInstanceState() (inside Activity, not View in this case) which will allow the activity to reset the state of it's view to the state it desires.

This should solve most of your worries. If you are wanting to use the onConfigurationChanged method, then you should reclarify your question as it is not clear what the current behavior is that you aren't expecting in each situation (only using onConfigurationChanged, or only using onCreate, or using both, etc).

Upvotes: 1

Tyler Ferraro
Tyler Ferraro

Reputation: 3772

You need to save and load the data you want to retain. Even though you're handling the screen rotation yourself when you modified the Manifest the way you did, you're still reloading the view yourself. Reread the reference document on Handling Runtime Changes. You need to store your data and reload it accordingly. Otherwise it will be lost when the application restarts or when you reload your ContentView.

http://developer.android.com/guide/topics/resources/runtime-changes.html

Upvotes: 2

Related Questions