Anthony Lo
Anthony Lo

Reputation: 17

Two activity cannot switch

In the 'MainActivity', I create a function about when I shake the device, it will go to the 'CompassActivity'. And it works. And I want to shake it again and want to back to 'MainActivity', but it doesn't work.

Here is the code of the MainActivity

package com.lab.mapme;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageButton;

public class MainActivity extends Activity implements SensorEventListener {

    ImageButton _buttonForward = null;
    ImageButton _buttonBackward = null;
    ImageButton _buttonLeft = null;
    ImageButton _buttonRight = null;

    boolean _fisMoved = false;
    boolean _bisMoved = false;
    boolean _lisMoved = false;
    boolean _risMoved = false;

    boolean _isShaked = false;
    long _lastSampleTime = 0;

    private SensorManager _sensorManager;

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            handleAccelerometer(event);
        }
    }

    private void handleAccelerometer(SensorEvent event) {
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];

        Acceleration_getCompress(x, y, z);
        if (_isShaked) {
            startActivity(new Intent(getApplicationContext(),
                    CompassActivity.class));
        }

        Acceleration_getDirectionForward(y);
        if (_fisMoved) {
            _buttonForward.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonForward.setBackgroundColor(Color.TRANSPARENT);
        }

        Acceleration_getDirectionBackward(y);
        if (_bisMoved) {
            _buttonBackward.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonBackward.setBackgroundColor(Color.TRANSPARENT);
        }

        Acceleration_getDirectionLeft(x);
        if (_lisMoved) {
            _buttonLeft.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonLeft.setBackgroundColor(Color.TRANSPARENT);
        }

        Acceleration_getDirectionRight(x);
        if (_risMoved) {
            _buttonRight.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonRight.setBackgroundColor(Color.TRANSPARENT);
        }
    }

    private void Acceleration_getCompress(float x, float y, float z) {
        float a = (x * x + y * y + z * z);
        float aToGravity = a
                / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long currentTime = System.currentTimeMillis();
        if (aToGravity >= 2) {
            if (currentTime - _lastSampleTime < 200) {
                // duration too short, ignore slight movement
                return;
            }
            _lastSampleTime = currentTime;
            _isShaked = !_isShaked;
        }

    }

    private void Acceleration_getDirectionForward(float y) {
        float a = y;
        if (a > a + y) {
            _fisMoved = !_fisMoved;
        }
    }

    private void Acceleration_getDirectionBackward(float y) {
        float a = y;
        if (a < a + y) {
            _bisMoved = !_bisMoved;
        }
    }

    private void Acceleration_getDirectionLeft(float x) {
        float a = x;
        if (a < a + x) {
            _lisMoved = !_lisMoved;
        }
    }

    private void Acceleration_getDirectionRight(float x) {
        float a = x;
        if (a > a + x) {
            _risMoved = !_risMoved;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        _buttonForward = (ImageButton) findViewById(R.id.buttonforward);
        _buttonBackward = (ImageButton) findViewById(R.id.buttonbackward);
        _buttonLeft = (ImageButton) findViewById(R.id.buttonleft);
        _buttonRight = (ImageButton) findViewById(R.id.buttonright);
    }

    @Override
    protected void onResume() {
        super.onResume();
        _sensorManager.registerListener(this,
                _sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        _sensorManager.unregisterListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ANd it is the code for the CompassActivity

package com.lab.mapme;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.Toast;

public class CompassActivity extends Activity {

    private SensorManager _sensorManager;
    private Sensor _sensor;

    private CompassView _compassView;

    boolean _isShaked = false;
    long _lastSampleTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        _sensor = _sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        _compassView = new CompassView(this);
        setContentView(_compassView);
    }

    private SensorEventListener _sensorEventListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            // angle between the magnetic north direction
            // 0=North, 90=East, 180=South, 270=West
            float azimuth = event.values[0];
            _compassView.updateData(azimuth);

            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                handleAccelerometer(event);
            }
        }

    };

    private void handleAccelerometer(SensorEvent event) {
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];

        Acceleration_getDirection(x, y, z);
        if (_isShaked) {
            startActivity(new Intent(getApplicationContext(),
                    MainActivity.class));
        }
    }

    private void Acceleration_getDirection(float x, float y, float z) {
        float a = (x * x + y * y + z * z);
        float aToGravity = a
                / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long currentTime = System.currentTimeMillis();
        if (aToGravity >= 2) {
            if (currentTime - _lastSampleTime < 200) {
                // duration too short, ignore slight movement
                return;
            }
            _lastSampleTime = currentTime;
            _isShaked = !_isShaked;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (_sensor != null) {
            _sensorManager.registerListener(_sensorEventListener, _sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            Toast.makeText(this, "ORIENTATION Sensor not found",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (_sensor != null) {
            _sensorManager.unregisterListener(_sensorEventListener);
        }
    }

}

Upvotes: 0

Views: 60

Answers (1)

Montycarlo
Montycarlo

Reputation: 735

Using startActivity(new Intent(getApplicationContext(), MainActivity.class)); will create a new activity instance - instead recycle your old activity by calling the onFinish() or onBackPressed() methods in place.

If that doesn't work ensure handleAccelerometer is being called when you shake your device during your CompassActivity.

Upvotes: 1

Related Questions