如何使用以前的InstanceState数据阻止活动重新启动

时间:2017-09-18 05:20:01

标签: android android-activity instancestate

我有一项从我的主要活动中打开的活动。当使用后退按钮关闭然后重新启动时,它将使用之前的instancestate打开,而不是打开,就好像它是新的一样。

主要活动

	public void onPerformButtonClick(View view)
	{
    	Intent performActivity = new Intent(getBaseContext(), PerformActivity.class);

    	//start lyric activity
        startActivityForResult(performActivity, MAIN_PERFORM_MODE);		
		
	}

PerformActivity

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setContentView(R.layout.perform_main);

		//reload state on orientation change or wake up
		if (savedInstanceState != null) {

			if (mPlayList == null) {
				mPlayList = new PlayList(getBaseContext());
			}
			mPlayList.removePlayListListener();
			mPlayList.setPlayListListener(new PlayListListener() {
				@Override
				public void onPlayListDataUpdate() {
					updateSetListData();
				}
			});
			mPlayList.loadState(savedInstanceState.getBundle("playlist"));

			if (mTimeLine == null) {
				mTimeLine = new TimeLine();
			}
			mTimeLine.removeTimeLineListener();
			mTimeLine.loadState(savedInstanceState.getBundle("timeline"));
		}

     .....

	}

	@Override
	protected void onSaveInstanceState (Bundle outState) {

		super.onSaveInstanceState(outState);

		Bundle playlist = mPlayList.saveState();
		outState.putBundle("playlist", playlist);

		Bundle timeline = mTimeLine.saveState();
		outState.putBundle("timeline", timeline);

	}

	private void doFinish()
	{
		finish();
	}

我确信这很简单,我不知道。

总结: 我希望应用程序在屏幕方向更改时表现良好,但当用户按下后退按钮时,我希望之前的状态消失。

1 个答案:

答案 0 :(得分:0)

在PerformActivity中添加此代码。这将完成按下后退按钮的活动。

@Override
public void onBackPressed() {
   if(null!=this){
       finish();
   }
   super.onBackPressed();
}