【问题标题】:Initializing variables in onCreate, but updating them in onResume()在 onCreate 中初始化变量,但在 onResume() 中更新它们
【发布时间】:2019-07-25 16:55:55
【问题描述】:

这是我的情况: 我有一个 OnCreate 代码,如下所示:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bunz = Bunz.getInstance(); //getting instance of bunz
        bunz.setBunz(50);
        bunz.setMoney(0);
        bunz.setIncrement(1);
        Button upgradeButton = (Button) findViewById(R.id.upgradeButton);
        upgradeButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent startIntent = new Intent(getApplicationContext(), UpgradeMenu.class);

                startActivity(startIntent);
            }
        });
        moneyCount = (TextView) findViewById(R.id.moneyCount);
        bunzCount = (TextView) findViewById(R.id.bunzCount);
        ImageButton bun = (ImageButton) findViewById(R.id.bun);

    }

请注意,在我的 OnCreate 代码中,我做了两件事;首先,我初始化所有需要的值:

        bunz.setBunz(50);
        bunz.setMoney(0);
        bunz.setIncrement(1);

然后我在 TextViews 上显示这些值并设置一些按钮和意图:

      Button upgradeButton = (Button) findViewById(R.id.upgradeButton);
        upgradeButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent startIntent = new Intent(getApplicationContext(), UpgradeMenu.class);

                startActivity(startIntent);
            }
        });
        moneyCount = (TextView) findViewById(R.id.moneyCount);
        bunzCount = (TextView) findViewById(R.id.bunzCount);
        ImageButton bun = (ImageButton) findViewById(R.id.bun);

我是 Android Studio 的新手,这就是我遇到的问题。每次我回到这个活动时,我想使用 onResume() 来更新 TextView 中的这些值(我在另一个活动中更新它们)。但是,如果我将 onCreate 中的所有代码移动到 onResume 中,那么每次我返回此活动时,值将设置为 50,0 和 1。我知道我可以使用布尔值,以便 onCreate() 触发第一次启动应用,但是onResume()没有触发,然后onResume()触发,然后简单的将onCreate代码的后半部分复制粘贴到onResume()中,但是这样看起来效率很低,而且是不是 Android Studio 的设计方式。我可以以某种方式初始化另一个位置的值吗?

我有一个全局 Bunz 类,如下所示:

public class Bunz {
    private int bunz;
    private int money;
    private int increment;
    //singleton code


    private static Bunz instance;
    private Bunz(){

    }
    public static Bunz getInstance(){
        if (instance == null){
            instance = new Bunz();
        }
        return instance;
    }

    public int getBunz() {
        return bunz;
    }

    public void setBunz(int num){
        bunz = num;
    }

    public int getMoney(){
        return money;
    }
    public void setMoney(int num){
        money = num;
    }
    public int getIncrement(){
        return increment;
    }
    public void setIncrement(int num){
        increment = num;
    }

}

所以也许我可以在这里以某种方式初始化这些值?

谢谢!

【问题讨论】:

  • 您能否解释一下您是如何在其他活动中更新这些值的,以及这些更新后的值是如何在 Resume 上加载的?如果你用这些代码编辑你的帖子会很好
  • 谢谢大家,从下面的答案中得到了解决方案! :)

标签: java android oncreate onresume


【解决方案1】:

你可以选择做一件事:

 public static Bunz getInstance(){
        if (instance == null){
            instance = new Bunz();
            instance.setBunz(50);
            instance.setMoney(0);
        }
        return instance;
    }

在此处创建实例时,请尝试在此处设置所需的值,而不是在应用的 onCreate 中。

您也可以在构造函数中进行更改。

【讨论】:

  • 该死的,非常感谢!我最终尝试创建另一个在 main 之前启动的活动来初始化值,但这是一个更好的解决方案! :D
【解决方案2】:

虽然您的代码使用静态,但我认为这是不必要的。静态不是一般的 goto 解决方案,它们带来了不符合 GC 条件的对象的高昂价格。

您可以通过onActivityResult 方法从第二个活动中获取结果。

首先,使用startAtivityForResult() 启动第二个活动 //这需要一个请求代码(Int),它可以是您设置的任何内容。

第一个活动

Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent , 100);

第二次活动

//Do you work in the second activity, generate new data

Intent returnIntent = new Intent();
returnIntent.putExtra("bunz", 100);
returnIntent.putExtra("money", 200);
returnIntent.putExtra("increment", 2);
setResult(Activity.RESULT_OK, returnIntent);
finish();

捕获第二个活动结果

这段代码应该写在你的第一个活动中。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 100) {  //Remember the code we set in startActivityForResult? This is where we identify our work
     if(resultCode == Activity.RESULT_OK){   //Code to check if data is passed
        Int bunz =data.getIntExtra("bunz")
        bunz.setBunz(bunz)

       .....
    }
  }
}

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多