Android方向改变奇怪的问题

时间:2011-02-22 16:44:10

标签: android

在更改模拟器的方向时,我有一些非常奇怪的问题。在我的应用程序中,当我点击一个图像按钮时,我会从我的主要活动中调用名为“设置”的活动。这是代码:

final ImageButton stbtn = (ImageButton) findViewById(R.id.stbtn);  
        stbtn.setOnClickListener(new OnClickListener() {  
             public void onClick(View v) {  
                Intent stopn = new Intent(MyApp.this, Settings.class);
                startActivity(stopn);
            }  
        });

以下是关闭设置活动的方法:

@Override
    public void onBackPressed() {
        // do something on back.
        finish();       
        return;
    }

@Override
    protected void onPause(){

       super.onPause();   
       saveSettings();

    }

在我的主要活动中,我有表面视图,每秒更新一次屏幕。我转到设置屏幕更改设置,然后按后退按钮关闭它,然后它进入主活动屏幕,这工作正常。现在的问题是当我在应用程序在主屏幕上更改方向时,它会打开设置活动。是因为我没有关闭活动,还是其他什么?有人可以帮我这个吗?感谢

编辑:

以下是mainfest和活动的代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myapp"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:name=".myappApp" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyAlarm"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                  android:configChanges="orientation"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Settings" android:label="@string/app_name">
        </activity>
    </application>
</manifest> 

主要活动代码:

public class MyAlarm extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getClockSettings();
        HandView hndView = (HandView) findViewById(R.id.hndView);
        clkapp = ((myappApp)getApplicationContext());
        hndView.setStyle(clkapp.getClkStyle(), clkapp.isAnalog());
        final ImageButton alrbtn = (ImageButton) findViewById(R.id.alrbtn);  
        alrbtn.setOnClickListener(new OnClickListener() {  
             public void onClick(View v) {  

             }  
        });  
        final ImageButton stbtn = (ImageButton) findViewById(R.id.stbtn);  
        stbtn.setOnClickListener(new OnClickListener() {  
             public void onClick(View v) {  
                Intent stopn = new Intent(MyAlarm.this, Settings.class);
                //stopn.setClassName("com.example.myapp", "com.example.myapp.Settings");
                startActivity(stopn);
            }  
        });

        stbtn.setOnTouchListener(new OnTouchListener() {  
           @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.requestFocusFromTouch();
                Intent stopn = new Intent(MyAlarm.this, Settings.class);
                   //stopn.setClassName("com.example.myapp", "com.example.myapp.Settings");
                startActivity(stopn);
                return true;
            }  
       });        


    }


    protected void getClockSettings() {
        //getting setting from preferences
    clkSettings = getSharedPreferences(PREFS_NAME, 0);      
        if (clkapp == null) clkapp = ((myappApp)getApplicationContext());
        clkapp.clkSettings = clkSettings;
        if (clkSettings == null) {
            /*clk24 = false;
            clkAnalogue = true;
            dSec = true;
            _clockStyle = "Black";*/
            clkapp.set24Hr(false);
            clkapp.setAnalog(true);
            clkapp.setDisplaySec(true);
            clkapp.setClkStyle("Black");
        }
        else {
            /*clk24 = clkSettings.getBoolean("24Hr", false);
            dSec = clkSettings.getBoolean("dSecond", true);
            clkAnalogue = clkSettings.getBoolean("ClkAnalogue", true);
            if (clkAnalogue) _clockStyle = clkSettings.getString("ClkAnalogStyle", "Black");
            else _clockStyle = clkSettings.getString("ClkAnalogStyle", "DigitalBlack");*/
            clkapp.set24Hr(clkSettings.getBoolean("24Hr", false));
            clkapp.setAnalog(clkSettings.getBoolean("ClkAnalogue", true));
            clkapp.setDisplaySec(clkSettings.getBoolean("dSecond", true));
            if (clkapp.isAnalog()) clkapp.setClkStyle(clkSettings.getString("ClkAnalogStyle", "Black"));
            else clkapp.setClkStyle(clkSettings.getString("ClkAnalogStyle", "DigitalBlack"));
        }
    }

    public void onConfigurationChanged(Configuration newConfig) {
          //ignore orientation change
          super.onConfigurationChanged(newConfig);
    }
}

设置活动:

public class Settings extends Activity {

    //Your member variable declaration here
    private RadioButton crbon;
    private RadioButton blue;
    private RadioButton bbl;
    private RadioButton crbdig;
    private RadioButton grndig;
    private ToggleButton tb24;
    private ToggleButton tbsec;

    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {
    //Your code here
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings); 
        myappApp clkapp = ((myappApp)getApplicationContext());
        //RadioGroup rbgrp = (RadioGroup)findViewById(R.id.rbfacegrp);

        crbon=(RadioButton)findViewById(R.id.radio_carbon);
        blue=(RadioButton)findViewById(R.id.radio_blue);
        bbl=(RadioButton)findViewById(R.id.radio_bbl);
        crbdig=(RadioButton)findViewById(R.id.radio_crbdig);
        grndig=(RadioButton)findViewById(R.id.radio_grndig);
        tb24=(ToggleButton)findViewById(R.id.tbhr);
        tbsec=(ToggleButton)findViewById(R.id.tbsec);
        if(clkapp.getClkStyle().toLowerCase().equals("black")) {
            crbon.setChecked(true);
        }
        else if(clkapp.getClkStyle().toLowerCase().equals("blue")) {
            blue.setChecked(true);
        }
        else if(clkapp.getClkStyle().toLowerCase().equals("bubble")) {
            bbl.setChecked(true);
        }
        else if(clkapp.getClkStyle().toLowerCase().equals("digitalblack")) {
            crbdig.setChecked(true);
        }
        else if(clkapp.getClkStyle().toLowerCase().equals("digitalgreen")) {
            grndig.setChecked(true);
        }
        tb24.setChecked(clkapp.is24Hr());
        tbsec.setChecked(clkapp.isDisplaySec());


    }

    @Override
    protected void onPause(){       
       super.onPause();   
       saveSettings();
        //this.finish();       
    }


    protected void saveSettings() {
        boolean clkAna = true;
        myappApp clkapp = ((myappApp)getApplicationContext());
        SharedPreferences.Editor edt = clkapp.clkSettings.edit();
        edt.putBoolean("24Hr", tb24.isChecked());
        edt.putBoolean("dSecond", tbsec.isChecked());
        clkapp.set24Hr(tb24.isChecked());
        clkapp.setDisplaySec(tbsec.isChecked());
        if (crbon.isChecked()) {
            edt.putString("ClkAnalogStyle", "Black");
            clkapp.setClkStyle("Black");
            clkAna = true;
        }
        else if (blue.isChecked()) {
            edt.putString("ClkAnalogStyle", "Blue");
            clkapp.setClkStyle("Blue");
            clkAna = true;
        }
        else if (bbl.isChecked()) {
            edt.putString("ClkAnalogStyle", "Bubble");
            clkapp.setClkStyle("Bubble");
            clkAna = true;
        }           
        else if (crbdig.isChecked()) {
            edt.putString("ClkAnalogStyle", "DigitalBlack");
            clkapp.setClkStyle("DigitalBlack");
            clkAna = false;
        }
        else if (grndig.isChecked()) {
            edt.putString("ClkAnalogStyle", "DigitalGreen");
            clkapp.setClkStyle("DigitalGreen");
            clkAna = false;
        }
        clkapp.setAnalog(clkAna);
        edt.putBoolean("ClkAnalogue", clkAna);
        edt.commit();

    }



    @Override
    public void onBackPressed() {
        // do something on back.
        finish();       
        return;
    }

}

1 个答案:

答案 0 :(得分:0)

当设备旋转时,它会杀死并重新创建活动,这是一个真正的痛苦。除非你真的需要这种行为,否则最简单的方法就是禁用它。要做到这一点,请在AndroidManifest中使用以下内容,告诉系统您将自己处理旋转。通常,这实际上不需要特殊的代码,它只是起作用。

<activity android:label="@string/my_activity_name" android:name="MyActivity" android:configChanges="keyboard|keyboardHidden|orientation" />