AndroidManifest.xml

android:configChanges 에 screenSize를 넣어줘야 onCreate를 다시 타지 않는다.         

 <activity  

          android:name=".MainActivity"

            android:label="@string/app_name" 

            android:configChanges="screenSize|keyboard|orientation"

            > 



Activity




import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private final String TAG = MainActivity.class.getSimpleName();
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			
		loadView();		
	}
	
	private void loadView() {
setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.rotate_button);
        btn.setOnClickListener(new View.OnClickListener() {
             
            @Override
            public void onClick(View v) {
                if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                    Log.i(TAG, "SCREEN_ORIENTATION_SENSOR_PORTRAIT");
                } else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    Log.i(TAG, "SCREEN_ORIENTATION_SENSOR_LANDSCAPE");
                }
            }
        });
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
          loadView();	
        } else {
            loadView();	
        }
    }

}






android:screenOrientation="unspecified"
위와 같은 내용을 적게 되면 핸드폰 설정값에 따라 방향전환이 일어나게 된다.






Posted by MR 손
,