开发者

Android orentation change weird problem

I have some really strange issue when changing orientation of my emulator. In my app I call activity named "Setting" from my main activity when an image button it clicked. here is the code for that:

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);
            }  
        });

Here is how I close Settings activity:

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

@Override
    protected void onPause(){

       super.onPause();   
       saveSettings();

    }

and in my main activity I have surface view that update screen every second. I go to Settings screen to change settings and press back button to close it and then it goes to the main activity screen this works fine. Problem now is that when I change orientation when the app is on main screen it opens Settings activity back. Is it because I haven't closed activity properl or is it something else? Can anyone please help me with this? Thanks

Edit:

Here is the code for mainfest and activities

<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> 

Main activity code:

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);
    }
}

Settings activity:

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
   开发者_JAVA百科 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;
    }

}


When the device rotates it kills and recreates the activity, which is a real pain. Unless you really need this behavior, the easiest thing to do is to disable it. To do this, use the following in your AndroidManifest, which tells the system that you will handle the rotations yourself. Generally this actually requires no special code, it just works.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜