在活动之间传递信息

Pass information between activities

我正在做一个必须对计步器进行编程的项目。计步器将使用一个按钮工作,您必须告诉您步数的长度。我制作了一个主要的 activity,让你在匿名和另一个让你注册的之间进行选择。该应用程序在我注册或匿名时有效,步长很好地传递给第三个 activity,一个 activity,您可以在其中按下按钮并增加完成的步数和仪表完毕。在这个 activity 中有另一个按钮可以配置您的步长,我想将所有信息传递给匿名 activity 以仅更改步长,我传递所有信息。我使用了 putExtra() 和 getIntent().getExtra().getString() 方法,但仅适用于从主 activity 到 registe/anonymous activity 然后转到计步器activity,但是当我想配置步长时,应用程序停止。

这是我的匿名代码 activity:

if(this.getIntent().hasExtra("name")){
        names=this.getIntent().getExtras().getString("name");
    }else{
        names="";
    }

    if(this.getIntent().hasExtra("user")){
        userName=this.getIntent().getExtras().getString("user");
    }else{
        userName="";
    }

    if(this.getIntent().hasExtra("pass")){
        password=this.getIntent().getExtras().getString("pass");
    }else{
        password="";
    }

    if(this.getIntent().hasExtra("feetBefore")){
        footBefore=this.getIntent().getExtras().getString("feetBefore");
    }else{
        footBefore="0";
    }

    if(this.getIntent().hasExtra("steps")){
        stepsDone=this.getIntent().getExtras().getString("steps");
    }else{
        stepsDone="0";
    }


    Button continueBtn = findViewById(R.id.continueAnonymous);
    foot =  findViewById(R.id.lengthFeetAnonymous);

    continueBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String footSize = String.valueOf(foot.getText());
            if(!footSize.equals("")) {
                Intent mainAnonymous = new Intent(getApplicationContext(), Main5Activity.class);
                mainAnonymous.putExtra("name", names);
                mainAnonymous.putExtra("user", userName);
                mainAnonymous.putExtra("pass", password);
                mainAnonymous.putExtra("feet", footSize);
                mainAnonymous.putExtra("steps", stepsDone);
                mainAnonymous.putExtra("feetBefore", footBefore);
                startActivity(mainAnonymous);
                finish() ;
            }else{
                Toast.makeText(Main4Activity.this, "You have to complete all the backets.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

这是我的计步器的代码activity:

Bundle parameters = this.getIntent().getExtras();
    if(parameters != null){
        name = parameters.getString("name");
        username = parameters.getString("user");
        password = parameters.getString("pass");
        String foot = parameters.getString("feet");
        String footBefore = parameters.getString("feetBefore");
        String stepsDone = parameters.getString("steps");
        if(stepsDone!=null) cont = Integer.parseInt(stepsDone);
        else cont =0;
        if(footBefore!=null)feetBefore = Integer.parseInt(footBefore);
        else feetBefore =0;
        if(foot !=null)feet = Float.parseFloat(foot)/100;
        else feet = (float) 0.43;
        cont2 = cont*feetBefore;
    }else {
        name = "";
        username = "";
        password = "";
        feet = (float) 0.43;
    }

    increase = findViewById(R.id.increaseStep);
    configuration = findViewById(R.id.confBtn);
    saveMain = findViewById(R.id.saveBtnMain);
    resume = findViewById(R.id.resumBtn);

    final TextView steps = findViewById(R.id.stepCounter);
    final TextView km = findViewById(R.id.kilometerCounter);
    steps.setText(String.format(Locale.ENGLISH,"%d Steps",cont));
    String aux =String.format(Locale.ENGLISH,"%.2f Meters", cont2);
    km.setText(aux);

    increase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont++;
            steps.setText(String.format(Locale.ENGLISH,"%d Steps",cont));
            cont2 += feet;
            String aux =String.format(Locale.ENGLISH,"%.2f Meters", cont2);
            km.setText(aux);
        }
    });

    configuration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent conf = new Intent(getApplicationContext(), Main4Activity.class);
            conf.putExtra("name", name);
            conf.putExtra("user", username);
            conf.putExtra("pass", password);
            String aux2 = String.valueOf(cont);
            conf.putExtra("steps", aux2);
            float aux4 =feet*100;
            String aux3 = String.valueOf(aux4);
            conf.putExtra("feetBefore", aux3);
            startActivity(conf);
            finish() ;
        }
    });
}

我昨天开始学习android所以我不知道我做错了什么。如果你能帮助我,我会很感激。另外,我觉得跟bundle有关。

将所有数据添加到一个包中,并且仅在 bundle!=null 时检查 –by Milan Pansuriya

我不知道使用捆绑包传递所有数据和将 putExtra 用于我的意图之间的区别,但这对我有用。谢谢 Milan Pansuriya。