以前称为活动/意图

时间:2014-03-08 20:05:33

标签: android android-intent android-activity

我正在尝试在Android中构建一个MP3播放器。我有一个带有3个按钮的MainActivity,如下所示:正在播放,我的歌曲和设置。每个按钮都有一个onClickListener。一切都有效,只有一个例外:当我点击MySongs ListActivity中的一首歌时,播放器被加载并开始播放歌曲。当我通过Android设备上的后退按钮返回主菜单,然后再次单击MySongs,然后选择一首新歌,加载一个新播放器并继续播放旧播放器,所以现在播放两首歌而不是只是新的。我认为问题在于我使用Intents的方式。

MainActiviy类(我省略了与此问题无关的代码)

public class MainActivity extends Activity {
    Player player = new Player();;

    @Override
    public void onCreate(Bundle sIS){
        super.onCreate(sIS);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.main);
    }

    ...
    public void launchMySongs(View v) {
        startActivity(new Intent(this, player.getClass()));
    }
    ...
}

现在是Player类(再次,我省略了一些我认为不重要的东西)

public class Player extends Activity implements OnCompletionListener, OnSeekBarChangeListener{
    private MediaPlayer mp;
    private Handler mHandler = new Handler();
    private SongsManager sm;
    private int currentSongIndex = 0;
    private boolean isPlaying = false;
    private boolean isShuffle = false;
    private boolean isRepeat = false;
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle sIS){
        super.onCreate(sIS);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.player);
        mp = new MediaPlayer();
        sm = new SongsManager();
        mp.setOnCompletionListener(this);
        songsList = sm.getSongList();
        startActivityForResult(new Intent(getApplicationContext(), SongsList.class), 100);
    }

    ...
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 100){
            currentSongIndex = data.getExtras().getInt(SongsManager.INDEX);
            playSong(currentSongIndex);
        }
    };
    ...

最后是SongList类

public class SongsList extends ListActivity {
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private String[] songTitles;
    MainActivity main;

    public void onCreate(Bundle sIS){
        super.onCreate(sIS);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.songslist);
        SongsManager sm = new SongsManager();
        this.songsList = sm.getSongList();
        this.songTitles = new String[songsList.size()];
        for (int i = 0; i < songsList.size(); i += 1){
            try {
                songTitles[i] = (String)songsList.get(i).get(SongsManager.KEY_TITLE);
            } catch (Exception e){
                Log.d("SongsList", "Class Cast Exception");
            }
        }
        this.setListAdapter(new SongListAdapter());
        ListView lv = this.getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                int songIndex = position;
                Intent i = new Intent(getApplicationContext(), Player.class);
                i.putExtra(SongsManager.INDEX, songIndex);
                setResult(100, i);
                finish();
            }
        });
    }
...

任何见解都将不胜感激。我正在努力做一些事情: Player.getIntent()或类似的东西,而不是调用新的Intent。

1 个答案:

答案 0 :(得分:2)

是的,问题在于,您无法确定您现在看到的Activity与您之前看到的Activity(在本例中为Player)的实例相同。 肯定不是您在MainActivity中创建的实例。您不应该创建Activity的实例。

您将不得不弄清楚(欢迎使用Android)如何将您的整个状态保留在用于启动新活动的意图中,或者保存在您正在调用的包中sIS。< / p>

查看Service类。我认为你想要让你的播放器成为一种服务,而不是一项活动。想想网络服务器。 Activity是一个servlet,即UI。中间件应该存在于服务中。