public class MainActivity extends BaseActivity { private String name; @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("name", name); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { name = savedInstanceState.getString("name"); } } }
import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MyActivity extends AppCompatActivity { private String valueOne; private int valueTwo; @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("valueOneKey", valueOne); outState.putInt("valueTwoKey", valueTwo); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); if (savedInstanceState != null) { valueOne = savedInstanceState.getString("valueOneKey"); valueTwo = savedInstanceState.getInt("valueTwoKey"); } } }This example demonstrates how to save multiple variables in the onSaveInstanceState method. The variables 'valueOne' and 'valueTwo' are of type String and integer respectively. These variables are saved in the bundle with their corresponding keys, and retrieved from the bundle in the onCreate method if there is a saved instance state. These examples belong to the android.os package.