在另一个activity之中选择选项,获取boolean值,返回mainactivity后调用onResume,在onResume中用获取的boolean判断应该变成那种功能(两个功能在一个acivity里写的 重力感应和非重力感应)但是发现在默认功能没有问题。 但是选择另一种模式的时候,两种功能的函数都被调用了,就还留在默认模式下,求解决!
主要代码如下:
public class MainActivity extends AppCompatActivity {
private TextView textView;
MyView mAnimView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout root = (LinearLayout) findViewById(R.id.root);
final DrawView draw = new DrawView(this);
draw.setMinimumWidth(300);
draw.setMinimumHeight(500);
root.addView(draw);
}
@Override
public void onResume(){
SharedPreferences modle = getSharedPreferences("MODEL", Context.MODE_PRIVATE);
boolean x = modle.getBoolean("model",true);
Log.v("现在的boolean为:"," "+x);
if(x==false){
LinearLayout root = (LinearLayout) findViewById(R.id.root);
Log.v("执行了1:"," "+x);
final MyView mAnimView = new MyView(this);
root.addView(mAnimView);
}
else{
LinearLayout root = (LinearLayout) findViewById(R.id.root);
final DrawView draw = new DrawView(this);
draw.setMinimumWidth(300);
draw.setMinimumHeight(500);
root.addView(draw);
Log.v("执行了2"," "+x);
}
super.onResume();
}