androidandroid-activityinstancestate

How to prevent activity re-starting with previous InstanceState data


I have an activity that is opened from my main activity. When it is closed using the back button and then restarted it is opening using the previous instancestate instead of opening as if it was new.

The Main activity

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

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

The 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();
	}

I am sure it is something simple that I am missing.

In summary: I want the app to behave nicely with screen orientation changes but when the user press the back button I want the previous state to be gone.


Solution

  • Add this code in PerformActivity.This wil finish the activity on pressing back button.

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