Android 会在最小化和最大化时复制活动

Android duplicates activity when minimized, then maximized

提问人:Ben 提问时间:1/20/2017 最后编辑:Ben 更新时间:3/17/2017 访问量:53

问:

我在谷歌上搜索了一下,但没有成功,接缝对我来说很奇怪。

我正在构建一个简单的 GPS 应用程序,该应用程序使用 HttpRequest 发送坐标,尽管我注意到在最小化 UI 然后最大化时,它运行了相同活动的副本。并在 HttpRequest 的

private LocationManager locationManager;

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

}


private void processExtraData() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(this);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            10000, 5, this);
}


    @Override
    public void onLocationChanged(Location location) {
        Bundle extras = getIntent().getExtras();
        String driver_id = extras.getString("driverid");

        String msg = "Driver:" + driver_id + "\nCurrent Location:\nLatitude: " + location.getLatitude()
                + "\nLongitude: " + location.getLongitude();

        new HttpRequestTask(
                new HttpRequest("https://www.autoflora.net/driver/gps.php?user_id=" + driver_id + "&latlong=" + location.getLatitude() + "*" + location.getLongitude(), HttpRequest.POST, "{ \"some\": \"data\" }"),
                new HttpRequest.Handler() {
                    @Override
                    public void response(HttpResponse response) {
                        if (response.code == 200) {
                            Log.d(this.getClass().toString(), "Request successful!");
                        } else {
                            Log.e(this.getClass().toString(), "Request unsuccessful: " + response);
                        }
                    }
                }).execute();
        String s = calc.getText().toString();
        calc.setText(s + "1")

        TextView driver = (TextView) findViewById(R.id.driver);
        driver.setText("" + driver_id);

        TextView Longitude = (TextView) findViewById(R.id.longitude);

        // Getting reference to TextView tv_latitude
        TextView Latitude = (TextView) findViewById(R.id.latitude);

        // Setting Current Longitude
        Longitude.setText("Longitude:" + location.getLongitude());

        // Setting Current Latitude
        Latitude.setText("Latitude:" + location.getLatitude());

        //  Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
    }
java android 活动

评论

0赞 Ben 1/22/2017
经过进一步调查,我相信是 requestLocationUpdates() 或 HttpRequestTask() 复制了它的自我......有什么解决办法吗?–
0赞 Code-Apprentice 1/22/2017
“最小化然后最大化”是什么意思?你点击哪些特定的按钮?
0赞 Ben 1/22/2017
不好意思。。应该已经更新了。当应用程序处于焦点中时
0赞 Code-Apprentice 1/22/2017
同样,您应该描述导致问题的确切操作

答:

1赞 Marcin Orlowski 1/20/2017 #1

在最小化 UI 然后最大化时注意到

Android 上既没有 UI,也没有 UI。您显然通过启动器再次启动活动(这很可能是您的事情),这会创建您的活动的新实例。如果无论如何都只允许单个实例,则必须通过在清单文件中使用 in 声明活动来设置系统。有关可能的选项,请参阅此处的文档minimisingmaximizingmaximizingandroid:launchMode

评论

0赞 Ben 1/20/2017
感谢您的输入 Marcin,不幸的是它没有奏效。我已经尝试了所有启动模式以及 intent.setFlags() 也没有成功。我确实注意到更改启动模式时存在一些差异,但是,除非我手动关闭整个应用程序,否则第二个活动总是会创建一个新活动
0赞 Ben 1/22/2017 #2
 @Override
    public void onStart() {
        super.onStart();
        Toast.makeText(getBaseContext(), "start", Toast.LENGTH_LONG).show();

        locationManager.removeUpdates(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getBaseContext(), "resume", Toast.LENGTH_LONG).show();
        locationcheck(); // checks permissions
    }

    @Override
    public void onPause() {
        super.onPause();
        Toast.makeText(getBaseContext(), "pause", Toast.LENGTH_LONG).show();


    }

    @Override
    public void onStop() {
        super.onStop();
     //   locationManager.removeUpdates(this);
        Toast.makeText(getBaseContext(), "stop", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getBaseContext(), "destroy", Toast.LENGTH_LONG).show();
        locationManager.removeUpdates(this);
        finish();

    }

似乎已经修复了它。.