0

In my activity I can drag the dot all over the screen. Once I press the Back button and then come back again to the activity the dot position isn't saved.. what should I do?

private ImageView map;
private ImageView dot;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);
    map = (ImageView) findViewById(R.id.map);
    dot = (ImageView) findViewById(R.id.dot);
    MotionReceiver tc = new MotionReceiver();
    LocalBroadcastManager.getInstance(this).registerReceiver(tc, new IntentFilter(Constants.TEXT_MOVEMENT));
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putFloat("X", dot.getX());
    savedInstanceState.putFloat("Y", dot.getY());
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    dot.setX(savedInstanceState.getFloat("X"));
    dot.setY(savedInstanceState.getFloat("Y"));
}
AxelH
  • 14,325
  • 2
  • 25
  • 55

3 Answers3

0

This is how you restore states:

private ImageView map;
private ImageView dot;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);
    map = (ImageView) findViewById(R.id.map);
    dot = (ImageView) findViewById(R.id.dot);
    MotionReceiver tc = new MotionReceiver();
    LocalBroadcastManager.getInstance(this).registerReceiver(tc, new 
    IntentFilter(Constants.TEXT_MOVEMENT));
    if(savedInstanceState != null){
       dot.setX(savedInstanceState.getFloat("X"));
       dot.setY(savedInstanceState.getFloat("Y"));
    }

 }

 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putFloat("X", dot.getX());
    savedInstanceState.putFloat("Y", dot.getY());
 }
Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
0

From documentation.

When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the instance state and is a collection of key-value pairs stored in a Bundle object.

So your problem is wrong understanding of onSavedInstanceState() method. It's called in case, when the system destroys your app, but not you.

To save some variables, that you'll need to restore after onDestroy() method invoke by pressing back button, you have to use SharedPreferences.

Dmitry Smolyaninov
  • 2,159
  • 1
  • 18
  • 32
-1

I have no clue what framework you're using, but these lines look suspect:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putFloat("X", dot.getX());
    savedInstanceState.putFloat("Y", dot.getY());
}

Try:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putFloat("X", dot.getX());
    savedInstanceState.putFloat("Y", dot.getY());
    super.onSaveInstanceState(savedInstanceState);
}
searlea
  • 8,173
  • 4
  • 34
  • 37
  • This is Android.not sure how it works in that case but the Bundle is used to save the state of an screen (aka Activity) if the screen is destroy (let say put in background and remove of memory to obtain to use it somewhere else). – AxelH Apr 25 '17 at 08:20
  • I'm using a framelayout.. it's not working –  Apr 25 '17 at 08:22
  • PS : unfortunatly, it need to be call as OP as writed. first the super method, then the setter. If not, the previous values are lost. – AxelH Apr 25 '17 at 08:25
  • @nbokmans are you talking specifically about Android? In java there are specific rules about calling `super()` first from a constructor, but... that doesn't apply to normal methods. – searlea Apr 25 '17 at 08:44