Android Activity 重启(由于屏幕方向改变)问题 stackoverflow 上提供的解决方案对我不起作用

标签 android android-layout android-orientation

我正在制作一个演示应用来说明我如何处理屏幕方向。这是一个简单的计数器应用程序,当用户按下递增按钮时递增计数,反之亦然。这是我的代码,我在 list 文件中包含了 android:configChanges="keyboardHidden|orientation"并且还覆盖了 onConfigurationChanged 方法。但不幸的是,我无法保留计数值,当我将模拟器切换到横向时,计数重置为 0,我该如何解决这个问题?

public class Counter_demoActivity extends Activity {

int count=0;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //UIActivity();
    Button add1=(Button)findViewById(R.id.btn1);
    Button sub1=(Button)findViewById(R.id.btn2);
    tv=(TextView)findViewById(R.id.tv1);
    add1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        count++;
        tv.setText("Your Count is " + count);
    }
});
   sub1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        count--;
        tv.setText("Your Count is " +count);
    }
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
    //UIActivity();
}
/*public void UIActivity(){
    this.tv=(TextView)this.findViewById(R.id.tv1);
    this.count=getChangingConfigurations();
}*/

我的 list 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arif.counter_demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    >
    <activity
        android:label="@string/app_name"
        android:name=".Counter_demoActivity"
        android:configChanges="keyboardHidden|orientation|keyboard" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

最佳答案

为什么你覆盖 onConfigurationChanged 而你不需要这样做......在你的情况下。不要删除 list 文件中的 android:configChanges="keyboardHidden|orientation" 只需删除代码

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
//UIActivity();
}

从你的项目中清理它并重建......

关于Android Activity 重启(由于屏幕方向改变)问题 stackoverflow 上提供的解决方案对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8222525/

相关文章:

android - 是否可以在 Android Studio 中删除我代码中的所有注释?

android - 415 XML Post Rest API 不支持的媒体类型

android - 什么使对话框可显示?

java - 更改圆弧半径

android - 控制相机以纵向拍照不会旋转最终图像

android - 无法保存和恢复嵌套 fragment ?

android - 在方向变化之间存储复杂的计算数据

java - 根据值对 TreeMap 进行排序?

java - 选择要更新的位置时如何正确使用Firestore ID

android - 当没有足够的空间时,如何使图像调整大小而不是文本消失?