儿童活动与父母活动之间的数据传输失败

时间:2019-06-17 13:28:32

标签: java android android-intent onactivityresult

我正在设计一种多功能记分板应用程序,其中每个活动都允许用户保持各种运动的得分。当前,有两个活动。主要活动支持篮球记分,而儿童活动则支持足球记分。每个活动都有一个按钮,允许用户在两​​个运动之间切换。当用户导航到子活动并将该数据显示在子活动的屏幕上时,我想将数据从父活动(篮球)发送到子活动(足球)。当用户导航回父活动时,我想将相同的数据发送回父活动。我使用了startActivityForResult(Intent I,int requestCode)和onActivityResult(int requestCode,int resultCode,Intent data)方法。

我已经进行了多次测试,并确认没有数据从子活动传输到父活动。我相当确定Android Studio环境不会调用onActivityResult()。我在该方法的最顶部放置了一个log命令,该消息未显示在logcat中。我也尝试了Build-> Clean Project。我关闭并重新打开工作室。我可以确认数据已成功从父方法发送到子方法。

这是jumpSoccer()方法,可简化父级->子级活动的切换。

 public void jumpSoccer(View view) {
        /*
        TESTING: SEND BASKETBALL DATA TO SOCCER ACTIVITY
        */

        //send basketball data
        Intent i = new Intent(this, Soccer.class);
        Bundle extrasBall = new Bundle();
        //team A info
        EditText teamA = findViewById(R.id.teamA);
        extrasBall.putString(EXTRA_TEAMA, teamA.getText().toString());
        TextView scoreA = findViewById(R.id.scoreA);
        extrasBall.putString(EXTRA_SCOREA, scoreA.getText().toString());
        //team B info
        EditText teamB = findViewById(R.id.teamB);
        extrasBall.putString(EXTRA_TEAMB, teamB.getText().toString());
        TextView scoreB = findViewById(R.id.scoreB);
        extrasBall.putString(EXTRA_SCOREB, scoreB.getText().toString());
        //add basketball bundle
        i.putExtras(extrasBall);

        //soccer bundle is global variable; initialized as null and given value in onActivityResult()
        //send soccer data back
        //if(extrasSoccer != null)
            //i.putExtras(extrasSoccer);
        startActivityForResult(i, SOCCER_REQUEST);
    }

这里是jumpBasketball方法,该方法有助于子->父活动切换。

  public void jumpBasketball(View view){

        Intent i = new Intent(this, MainActivity.class);

        //get bundle sent from basketball activity and add to intent to send  back
        Bundle extrasBall = this.getIntent().getExtras();
        i.putExtras(extrasBall);
         startActivity(i);

注意:由于与该问题无关的原因,getIntent()在此方法之前被调用一次

这里是onActivityResult方法,该方法应该处理子活动返回的数据。

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //receive data and populate stat page (artificial memory) for basketball data and simply hold soccer data until it is returned
        Log.d("oAR called", "success!");
        super.onActivityResult(requestCode, resultCode, data);

        //request code indicates origin of data
        if (resultCode == RESULT_OK) {

            if (requestCode == SOCCER_REQUEST) {
                //get basketball bundle
                Bundle extrasBall = data.getExtras();
                //repopulate team A info
                EditText teamA = findViewById(R.id.teamA);
                teamA.setText(extrasBall.getString(EXTRA_TEAMA));
                TextView scoreA = findViewById(R.id.scoreA);
                scoreA.setText(extrasBall.getString(EXTRA_SCOREA));
                //repopulate team B info
                EditText teamB = findViewById(R.id.teamB);
                teamB.setText(extrasBall.getString(EXTRA_TEAMB));
                TextView scoreB = findViewById(R.id.scoreB);
                scoreB.setText(extrasBall.getString(EXTRA_SCOREB));

            }
        }

        }

我希望父活动显示子活动中的数据,但不会。似乎由于logcat中缺少上述日志消息而未触发onActivityResult()。

1 个答案:

答案 0 :(得分:0)

在您的子活动中,使用它返回到父活动

Intent data = new Intent();
data.putExtra("bundleName", yourBundle);
setResult(RESULT_OK, data);
finish();