When I change the orientation from portrait to landscape, my calculated result disappears. how to solve it?

Hai, in my calculator application, when I click on the calculate button, the result appears normally, but when I change the orientation, the calculated result disappears.

+6
source share
3 answers

See this example on how to keep your business running using the Bundle . First you must 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 restore the state of your instance from a saved package.

 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
source

Try this code

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

and in 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 do not add this flag, the onConfigurationChanged method will not be called.

+3
source

activity restarts after changing orientation. you need to save the current values ​​in the package

(I assume you mean the Android app)

0
source

Source: https://habr.com/ru/post/885635/


All Articles