简体   繁体   English

直到再次调用Main Activity才更新Sub Activity的结果

[英]Main Activity is not updating with Sub activity's result until called again

I'm trying to update a button's text by using an intent with a subactivity to debug why my player was throwing an exception, but it returns null until I call the subactivity again where it updates with the result of the first call. 我正在尝试通过使用具有子活动的意图来调试按钮的原因来调试我的播放器引发异常的原因,但是它会返回null,直到我再次调用该子活动,并在第一次调用的结果处进行更新。

Main Activity 主要活动

public class MainActivity extends AppCompatActivity {

    static final int PICK_AUDIO_REQUEST = 1;
    String ResultPath;
    String path;
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Sound sound = new Sound(R.id.button,this);

        sound.button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(sound.mp == null){
                    Intent intent = new Intent(MainActivity.this, Pop.class);
                    startActivityForResult(intent, PICK_AUDIO_REQUEST);
                    //Pass audio file into sound
                    /*try {
                        sound.mp.setDataSource(path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/
                }
                else if(sound.mp.isPlaying()) {
                    sound.mp.pause();
                }
                else{
                    sound.mp.seekTo(0);
                    sound.mp.start();
                }
            }


        });



    }

Subactivity 子活动

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Intent resultIntent = new Intent();
                resultIntent.putExtra("song path", mMusicList[arg2]);
                setResult(Activity.RESULT_OK, resultIntent);
                finish();
            }
        });

Example: 例:

Intialization: 初始化:

Subroutine is called and I select ring.wav from the popup 调用子例程,然后从弹出窗口中选择ring.wav

Afterwards, selecting ring.wav will not update the button with the correct path until you click on the button again. 之后,选择ring.wav不会使用正确的路径更新按钮,除非再次单击该按钮。

onActivityResult onActivityResult

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            case (PICK_AUDIO_REQUEST) : {
                if (resultCode == Activity.RESULT_OK) {

                    ResultPath = data.getStringExtra("song path");
                    path = extStorageDirectory + File.separator + ResultPath;

                }
                break;
            }
        }
    }

I need to see more code (eg) onActivityResult , where is path variable, etc. 我需要查看更多代码(例如onActivityResult ,其中path变量等。

So far, I believe that on the onActivityResult you should update the text. 到目前为止,我相信您应该在onActivityResult更新文本。

Check here . 在这里检查。

Maybe, you add "button.setText()" in onActivityResult. 也许您在onActivityResult中添加了“ button.setText()”。

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case (PICK_AUDIO_REQUEST) : {
            if (resultCode == Activity.RESULT_OK) {

                ResultPath = data.getStringExtra("song path");
                path = extStorageDirectory + File.separator + ResultPath;
                //Change button with audio file's path
                sound.button.setText(path);

            }
            break;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的适配器不会更新值,直到我返回然后再次返回该活动。 什么原因 - My adapter does not updating value until i go back and then again come back to that activity. What's the reason 在主要活动中更新时钟 - Updating a clock in main activity 在活动A之上启动活动B(由应用程序类的子类的onCreate调用)(由活动类的子类的onResume调用) - start Activity B(called by onCreate of an Application Class's sub Class) on Top of Activity A(called by onResume of an Activity Class's sub Class) 上一个活动的方法在onBackPress上再次被调用 - previous activity's method being called again onBackPress 主要活动不等待子活动完成 - main activity does not wait for sub activity to finish 即使子活动执行setResult()和finish(),也不会调用主活动中的onActivityResult - onActivityResult in the main activity is not called even though the sub-activity executes setResult() and finish() Android:在主活动容器中用其父片段替换子片段 - Android: Replace sub fragment with it's parent fragment in main activity container 我的主要活动中未调用onActivityResult() - onActivityResult() is not called in my main activity 如果结果活动中启动了新的活动,则不会调用OnActivityResult - OnActivityResult is not called if new Activity is started in result activity 为什么主活动在其调用意图完成后会重新启动(Android 4.2.2)? - Why does the main activity restart again after its called intent finished (Android 4.2.2)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM