按下最近的按钮或后退按钮时停止活动?

时间:2017-07-23 23:35:28

标签: java android android-activity activity-finish

在我的应用程序中,当我按下后退按钮或最近按钮(显示应用程序)并返回到应用程序时,用户得分的值开始加倍原始值。

我认为它与onResume在后台运行有关,当他们重新进入应用程序时,它会运行两次。我试过了:

package com.example.navjeevenmann.mytycoon;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Button myButton;
    private int Counter;
    private Button myButton2;
    private TextView myTextView;
    Handler handler = new Handler();
    private int add;
    private TextView myAddView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.containsKey("Count")) {
                Counter = bundle.getInt("Count");
            }
            if (bundle.containsKey("Add")) {
                add = bundle.getInt("Add");
            }
        }

        myButton = (Button) findViewById(R.id.button);
        myButton2 = (Button) findViewById(R.id.button2);
        myTextView = (TextView) findViewById(R.id.textView);
        myAddView = (TextView) findViewById(R.id.textView2);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Counter = ButtonCounter(Counter);
            }
        });
        myButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),                 SecondActivity.class);
                intent.putExtra("Count", Counter);
                intent.putExtra("Add", add);
                startActivity(intent);
            }

        });

    }

    public void onResume() {
        super.onResume();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Counter = AutoCounter(Counter, add);
                handler.postDelayed(this, 1000);
            }
        }, 1000);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Display(Counter, add);
                handler.postDelayed(this, 1);
            }
        }, 50);
    }



    public int ButtonCounter(int Counter) {
        Counter += 1;
        return Counter;
    }

    public int AutoCounter(int Counter, int add) {
        Counter = Counter + add;
        return Counter;
    }

    public void Display(int Counter, int add) {
        String money = String.valueOf(Counter);
        myTextView.setText("$" + money);
        myAddView.setText("Generating $" + add + "/Per Second");

    }

}

但是结束了活动,当用户进入第二个应用程序时,用户转到第二个活动而不是主活动。

以下是该活动的代码:

{{1}}

0 个答案:

没有答案