user1150750
user1150750

Reputation: 1

viewFlipper save position

I am trying to find a way to make a viewflipper open to the last child position that it was at when the app was closed. Any help would be appreciated. I am still new at this.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);

    Toast.makeText(this, "Swipe Left or Right", Toast.LENGTH_LONG).show();       

    gestureDetector = new GestureDetector(new MyGestureDetector());
    View mainview = (View) findViewById(R.id.viewFlipper1);

     // Set the touch listener for the main view to be our custom gesture listener
    mainview.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    });
}

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float          `enter code here`velocityY) {        
    final ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);

        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
            return false;
        }

Upvotes: 0

Views: 357

Answers (1)

Basil
Basil

Reputation: 2173

I think you can use the SharedPreferences in Android to save the position of flipper that was viewed lastly before the application was closed. So whenever the application moves to this activity, you can retrieve the position of the flipper from the SharedPreference and call property of flipper: viewFlipper.setDisplayedChild(position retrived from SharedPreference); This will help you to move the flipper to the position that was viewed lastly before the application was closed. I hope this will help you to solve the issue.

Upvotes: 1

Related Questions