Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with member variable of an activity when using back button
    primarykey
    data
    text
    <p>I've got an activity that shows some item from a list.</p> <p>When swiping from right to left the same activity is started with the next product and when swiping from left to right I start the same activity with the previous product.</p> <p>I get the product position from a global array using a member variable <code>position</code> that I decrement when going to previous product and increment when going to next product, and that I pass as an extra in the intent. </p> <p>The issue is that when pressing the back button, the <code>position</code> value remains the same as that in the activity where I'm coming from. I understand that onResume is called instead of onCreate and that no extra is passed, but <code>position</code> is not static and I expect it to keep its value in the activity instance. Why is this not the case?</p> <p>Thanks</p> <pre><code>public class ProductHome extends Activity{ private int position; private Product product; private GestureDetector gestureDetector; View.OnTouchListener gestureListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.product_home); position = getIntent().getIntExtra("Position", -1); product = GlobalData.map_products.get(product_id); // swipe detector // left -&gt; right: position++ // right -&gt; left: position-- // then starts same activity again gestureDetector = new GestureDetector(new SRPGestureDetector()); gestureListener = new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } else{ return false; } } }; RelativeLayout product_home = (RelativeLayout) findViewById(R.id.product_home); product_home.setOnTouchListener(gestureListener); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload