when i change the orientation from portrait to lan

2019-01-25 13:20发布

问题:

Hai, in my calculator app when i click on calculate button result is appearing normally, but when i change the orientation calculated result is disappearing.

回答1:

Please see this example on how to save the state of your Activity using a Bundle. First you have to override the onSaveInstanceState method.

public void onSaveInstanceState(Bundle savedInstanceState) {
    TextView  txtName = (TextView)findViewById(R.id.raj44);
    String  aString = txtName.getText().toString();
    savedInstanceState.putString("Name", aString);
    super.onSaveInstanceState(savedInstanceState);
} 

In the onCreate method you can then restore the state of your instance from the saved Bundle.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.concretetool);
    if (savedInstanceState != null) {
        String  aString = savedInstanceState.getString("Name");
        if (aString != null) {
            txtName = (TextView)findViewById(R.id.raj44);
            txtName.setText(aString);
        }
    }
}


回答2:

Try this code

@Override  
public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig);  
}

and in the manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">´
    <activity android:name="XXXXX"
              android:label="@string/app_name"
              android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> //this <--
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    ...
</application>

EDIT: I added the screenSize flag. In android >3 if you dont add this flag the method onConfigurationChanged will not be called.



回答3:

the activity is restarted after the orientation changed. you have to save the current values in bundle

(I assume you mean an android app)