0

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.

Dan
  • 15
  • 3
  • do you want to restart that activity with fresh data – Pranav Ashok Sep 18 '17 at 06:31
  • Every time i press the button that runs onPerformButtonClick i would like a new fresh instance of performActivity to run. Without any of the previous instanceState data loading – Dan Sep 18 '17 at 21:30
  • Are there maybe Intent FLAGs that can be used to create the performActivity each time without a previous instancestate? – Dan Sep 18 '17 at 22:25
  • So after further testing it looks like its not instancestate that is being reloaded but rather, the previous performActivity is still there (with all its data) and is re-activated. I need to be able to fully finish and remove the instance completely – Dan Sep 18 '17 at 22:34
  • The issue was that mPlayList is a static variable and was not being reset to null when the activity was closed. Sorted now. Thanks for the help. – Dan Sep 18 '17 at 23:56

1 Answers1

0

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

@Override
public void onBackPressed() {
   if(null!=this){
       finish();
   }
   super.onBackPressed();
}
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • Sorry but this didnt work. Yes the activity finished but when onPerformButtonClick in the main activity is pressed again and performActivity is started again it loads with its previous instancestate instead of loading fresh – Dan Sep 18 '17 at 22:22
  • The issue was due to a static variable not being nulled. Technically though, your answer was correct for the question I posted. – Dan Sep 18 '17 at 23:57
  • ohh I dint see any static variable mPlayList in your code.Any ways happy to hear it helped you. – Sharath kumar Sep 19 '17 at 04:42