Save Android Studio UI interface and screen orientation state

com.example.orientation Package; 

Import android.os.Bundle; 
Import android.util.Log; 
Import android.view.View; 
Import android.widget.Button; 
Import android.widget.TextView; 

Import androidx.appcompat.app.AppCompatActivity ; 

public class the MainActivity the extends AppCompatActivity { 
/ * 
    = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
    the present example the main study, the screen is inverted, how adaptive interface, horizontal screen layout created 
    1. prohibit switching horizontal screen: in AndroidManifest.xml - provided the following code> application-> activity-> of ( android: screenOrientation = "portrait") 
      <Activity Android: Android name = "the MainActivity.": screenOrientation = "Portrait"> 
    2. create a landscape layout, horizontal screen, it will automatically load the landscape layout interface (manifest file, pay attention to remove android: screenOrientation = "portrait") 
    3. flip the screen, save window controls state value;
 
    = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 * / 
    the Button Button; 
    the TextView the textView ; 

    String the TAG = "myTag"; 

    @Override 
    protected void the onCreate (the Bundle savedInstanceState) { 
        super.onCreate (savedInstanceState); 
        the setContentView (R.layout.activity_main); 

        Button = the findViewById (R.id.button); 
        the textView the findViewById = (R & lt .id.textView); // if the value of the State is not empty, if the value of the corresponding component, is read out up assignment 
        IF (savedInstanceState = null)! 
        {

        
            String s = savedInstanceState.getString("key");
            textView.setText(s);
        }

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(button.getText());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy:");
    }

    @Override
    //将 textView 中的值,先保存到 outState 中(键值对)
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("key",textView.getText().toString());
    }
}

  Project: Orientation

Guess you like

Origin www.cnblogs.com/gfwei/p/11770553.html