java - Android 在最小化时复制 Activity,然后最大化

标签 java android android-activity

我用谷歌搜索了这个但没有成功,接缝对我来说很奇怪。

我正在构建一个简单的 GPS 应用程序,它使用 HttpRequest 发送坐标,尽管我注意到在最小化 UI 然后最大化时,它会运行相同 Activity 的副本。并在 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();
    }

最佳答案

noticed when minimising the UI then maximising

Android 上的 UI 既没有最小化也没有最大化。您显然通过启动器(这很可能是您的 maximizing 东西)再次启动 Activity ,这会创建您 Activity 的新实例。如果无论如何都只允许单个实例,则必须通过在 list 文件中的 Activity 声明中使用 android:launchMode 来设置系统。参见 docs here可能的选择。

关于java - Android 在最小化时复制 Activity,然后最大化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41755618/

相关文章:

java - 在 Android 应用程序中将一项 Activity 的分数转化为另一项 Activity 的分数

android - 使用 imageview (Android) 滚动宽图像

java - 将 Java 对象排序到桶中,然后在桶内排序的算法

java - 使用 pinterest 应用程序打开 pinterest 链接(如果存在)- android

android - 动画和 setVisibility

android - 如何将点击监听器添加到我的回收站 View (Android kotlin)

java - 构造函数注入(inject)找不到符号方法注入(inject)成员

java - 无法让我的组合框和按钮工作。实现带有按钮的框时出现问题

android - Google map 和列表合二为一

android - 关于 toast 和 Activity 更改的 WindowManager$BadTokenException