The application is not correct after the change of screen orientation.



  • There's an app on android, it's got four buttons on it, and each sends its data through Socket. The annex is correct, but after the change of screen orientation, the data of the button 4 shall be sent to the button 1 when applied.

    Button b1;
    Button b2;
    Button b3;
    Button b4;
    Button connection;
    Button disconnection;
    TextView IpAddress;
    TextView NewPort;
    

    int b;

    private Socket socket;

    private int SERVERPORT;
    private String SERVER_IP;

    ClientThread client;

    private Timer mTimer;
    private MyTimerTask mMyTimerTask1;
    private MyTimerTask mMyTimerTask2;
    private MyTimerTask mMyTimerTask3;
    private MyTimerTask mMyTimerTask4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    b1 = (Button) findViewById(R.id.b1);
    b2 = (Button) findViewById(R.id.b2);
    b3 = (Button) findViewById(R.id.b3);
    b4 = (Button) findViewById(R.id.b4);
    connection = (Button) findViewById(R.id.connection);
    disconnection = (Button) findViewById(R.id.disconnection);
    IpAddress = (TextView) findViewById(R.id.IpAdd);
    NewPort = (TextView) findViewById(R.id.PortAdd);
    
    connection.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                SERVER_IP = IpAddress.getText().toString();
                SERVERPORT = Integer.parseInt(NewPort.getText().toString());
    
                client = new ClientThread();
                Thread thread = new Thread(client);
                thread.start();
    
                disconnection.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
    
                b1.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        b = 1;
                        if (mTimer != null) {
                            mTimer.cancel();
                        }
                        mTimer = new Timer();
                        mMyTimerTask1 = new MyTimerTask();
    
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            mTimer.schedule(mMyTimerTask1, 0, 250);
                        }
                        if (event.getAction() == MotionEvent.ACTION_MOVE) {
                            mTimer.schedule(mMyTimerTask1, 0, 250);
                        }
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            mTimer.cancel();
                        }
                        return false;
                    }
                });
    
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    });
    

    }

    class ClientThread implements Runnable {
    PrintWriter out;

    @Override
    public void run() {
                try {
                    InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                    socket = new Socket(serverAddr, SERVERPORT);
    
                    out = new PrintWriter(socket.getOutputStream(), true);
                } catch (UnknownHostException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
    }
    
    public void sendMessage(String str) {
        out.write(str);
        out.flush();
    }
    

    }

    class MyTimerTask extends TimerTask {

    @Override
    public void run() {
        if (b == 1) mMyTimerTask1();
        if (b == 2) mMyTimerTask2();
        if (b == 3) mMyTimerTask3();
        if (b == 4) mMyTimerTask4();
    }
    

    }

    private void mMyTimerTask1() {
    client.sendMessage("F"); //1
    }



  • Try adding onSaveInstanceState. http://startandroid.ru/ru/uroki/vse-uroki-spiskom/133-urok-70-onsaveinstancestate-sohranenie-dannyh-activity-pri-povorote-ekrana




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2