Android Studio Value不会传递给其他活动

时间:2017-03-14 14:44:32

标签: java android

我有问题。我这里有一个简单的点击器,但“每次点击升级的点击次数”不会转移到其他活动。我不知道为什么它不起作用。

代码如下:

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

public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_CODE_CURRENTCLICKS = 10;
   public int amountClicks = 0;
   public int incrementAmount = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnClick = (Button) findViewById(R.id.btnClick);
        final TextView tvAmountClicks = (TextView) findViewById(R.id.tvAmoutClicks);
        Button btnShop = (Button) findViewById(R.id.btnShop);

        amountClicks = getIntent().getIntExtra("currentClicks", 0);
        incrementAmount = getIntent().getIntExtra("upgradedClicks", 1);
        tvAmountClicks.setText(""+amountClicks);


        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                amountClicks += incrementAmount;
                tvAmountClicks.setText(""+amountClicks);

            }
        });
        btnShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),ShopActivity.class);
                intent.putExtra("currentClicks",amountClicks);
                startActivityForResult(intent, REQUEST_CODE_CURRENTCLICKS);
            }
        });
    }
}
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShopActivity extends AppCompatActivity {
  public int currentClicks;
   public int incrementAmount;
   public int upgradedClicks;
    public int upgradeCost = 10;

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

        currentClicks = getIntent().getIntExtra("currentClicks",0);

        final TextView tvCurrentClicks = (TextView) findViewById(R.id.tvCurrentClicks);
        tvCurrentClicks.setText(""+currentClicks);

        Button btnUpgrade = (Button) findViewById(R.id.btnUpgrade);
        btnUpgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(upgradeCost > currentClicks)
                {
                    Context context = getApplicationContext();
                    CharSequence text = "You don't have enough Clicks";
                    int duration = Toast.LENGTH_SHORT;
                    Toast cantUpgrade = Toast.makeText(context, text,duration);
                    cantUpgrade.show();
                }
                else
                {
                  currentClicks -= upgradeCost;
                    upgradedClicks++;
                    tvCurrentClicks.setText(""+ currentClicks);

                }
                Intent intent = new Intent();
                intent.putExtra("currentClicks",currentClicks);
                intent.putExtra("upgradedClicks",upgradedClicks);

                setResult(RESULT_OK, intent);

            }
        });

    }
}

2 个答案:

答案 0 :(得分:3)

  

如果我返回主要活动,则不会返回“已升级的点击次数”或“当前点击次数”。

您有startActivityForResult因此需要实施onActivityResult

所以,你有这个代码

Intent intent = new Intent();
intent.putExtra("currentClicks",currentClicks);
intent.putExtra("upgradedClicks",upgradedClicks);

setResult(RESULT_OK, intent);
finish(); // Add finish() to end this Activity

这将返回到调用请求代码的调用Activity。

public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_CODE_CURRENTCLICKS = 10;
    public int amountClicks = 0;
    private TextView tvAmountClicks;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == REQUEST_CODE_CURRENTCLICKS) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {

                amountClicks = data.getIntExtra("currentClicks", 0);
                int upgradedClicks = data.getIntExtra("upgradedClicks", 1);
                tvAmountClicks.setText(""+amountClicks);

            }
        }
    }

Android | Getting the result of an Activity

答案 1 :(得分:0)

您应该覆盖MainActivity的{​​{1}}方法来刷新数据。

编辑试试这个

onResume()