使用onActivityResult启动第一个Activity

时间:2013-12-21 15:03:09

标签: android eclipse android-intent set

我想打开另外两个活动,然后我想在活动A中播放活动C中的选定歌曲。活动 A 我打开活动 B 并在那里我打开一个活动 C 。 我想在活动 C 中播放选定的歌曲。

但我不明白。怎么了? 非常感谢,Vinzenz:)


这是ActivityA:

LinearLayoutBrowse.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
    Intent i = new Intent(getApplicationContext(), BrowseActivity.class);
        startActivityForResult(i, 100); 

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
              @Override
              public void run() {
                  mDrawerLayout.closeDrawers();
              }
            }, 300);

        }
    });

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

    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == 100){
         currentSongIndex = data.getExtras().getInt("songIndex");

         //drawerclose


         // play selected song
         playSong(currentSongIndex);

    }

}

这是ActivityB:

      LinearLayoutTitel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            myViewFlipper.showPrevious();
            Intent i = new Intent(BrowseActivity.this, AllSongs.class);
            startActivityForResult(i,  100);

        }
    });


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // this is the data that comes back from Activity C when Activity C calls finish()

    // set the data as the result
    setResult(???);

    // then you can finish
    finish();
}

这是ActivityC:

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting listitem index
            int songIndex = position;

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    MainActivity.class);


            // Sending songIndex to BrowseActivity
            in.putExtra("songIndex", songIndex);

            setResult(100, in);
            // Closing PlayListView
            finish();
        }

1 个答案:

答案 0 :(得分:1)

在您的第三个Activity中,Intent构造函数应为空。而不是

Intent in = new Intent(getApplicationContext(), MainActivity.class);

应该是

Intent in = new Intent();

否则,您正在创建MainActivity的新实例,并会调用其onCreate()