使用后退按钮,当问题与活动的成员变量 [英] Issue with member variable of an activity when using back button
问题描述
我有一个活动,显示了一个列表中的某些项目。
当由右至左的相同的活性刷卡开始与下一个产品和从左至右刷时予先从previous产品相同的活性
我从一个全局数组使用一个成员变量的产品定位位置
,我递减去下一个产品时,将previous产品和增值的时候,和我通过作为目的的额外费用。
问题是,当pressing后退按钮时,位置
值保持相同,在我来自的活动。据我所知,onResume被称为替代的onCreate,并没有多余的传递,但位置
不是静态的,我希望它保持其价值的活动实例。这是为什么不是这样?
感谢
公共类产品展示首页延伸活动{
私人诠释的位置;
私人产品的产品;
私人GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.product_home);
位置= getIntent()getIntExtra(位置,-1)。
产品= GlobalData.map_products.get(产品);
//刷卡器
//左 - >右:位置++
//正确的 - >左:position--
//然后再次开始相同的活动
gestureDetector =新GestureDetector(新SRPGestureDetector());
gestureListener =新View.OnTouchListener()
{
公共布尔onTouch(视图V,MotionEvent事件)
{
如果(gestureDetector.onTouchEvent(事件))
{
返回true;
}
其他{
返回false;
}
}
};
RelativeLayout的product_home =(RelativeLayout的)findViewById(R.id.product_home);
product_home.setOnTouchListener(gestureListener);
}
}
这已经跟你启动您的活动模式。最有可能的,有你的活动只有一个实例。
吊起到右侧时,您可能不应该推出新的活动,可能不会使用后退按钮返回到previous产品,而是到列表中,你可能COM从
I've got an activity that shows some item from a list.
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.
I get the product position from a global array using a member variable position
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.
The issue is that when pressing the back button, the position
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 position
is not static and I expect it to keep its value in the activity instance. Why is this not the case?
Thanks
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 -> right: position++
// right -> 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);
}
}
This has to do with the mode you launch your activity in. Most likely, there is only one instance of your activity.
You probably should not launch a new activity when slinging to the right, and probably not use the back button to go back to the previous product, but rather to the list where you probably com from
这篇关于使用后退按钮,当问题与活动的成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!