Dealing with SavedInstances, and restoration of ac

2020-05-02 13:05发布

basically my app has 2 activities.Say "A" and "B" . A launches B.

Activity B plays music and also has a notification.

Case 1:when the view is still on activity B` i press home button and then i click on the the notification, activity B is opened with its view intact and with its music playing (because in the manifest i am using android:launchMode= "singleTop" and hence another instance of the activity is not created) this part is as desired...... but

Case 2:when the view is on activity B and i press back button ,activity A appears and then i click on the the notification, activity B is opened with the view lost and music also stops(not desired)......i am making a guess that it happens because when i press the back button the activity is destroyed ,so i have to progamatically restore its view right??so to restore its view i override two methods .....

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("boolMusicPlaying", boolMusicPlaying);
  savedInstanceState.putInt("swapnumber", swapnumber);
  savedInstanceState.putString("seekbarprogress", progress2);
  savedInstanceState.putInt("position.seekbar",seekbar.getProgress());
  savedInstanceState.putString("seekmaxString", max2);
  savedInstanceState.putInt("seekmaxInt",seekMax);
  savedInstanceState.putParcelableArrayList("songfetails",songdetails);
  super.onSaveInstanceState(savedInstanceState);


}
//make a note ....even if i don't override onDestroy() and don't call on SaveInstanceState explicitly, then too i am not getting any desired effect.......
@Override
public void onDestroy()
{   Bundle savedState = new Bundle();
onSaveInstanceState(savedState);//because of this line....the app is crashing......
super.onDestroy();
    }

but it didn't help..... and in on create i am putting a check if saved instances is null or not....create view accordingly...(i.e from saved instances or from fresh) but it didn't help... also this line is giving a crash ...onSaveInstanceState(savedState);

even if i don't override ondestroy() and kill the app manually from task killer,and then try to open activity B,then too the saved instances thing should work right,because the method OnSaveInstanceState will be automatically called then,right???please help

标签: android
2条回答
老娘就宠你
2楼-- · 2020-05-02 13:36

Basically if you have pressed a back button,restoration of activity should be done using shared preferences/or a database but if you haven't pressed the back button and then you want to restore the state of the activity (because the activity was destroyed by the system ) then the bundle savedinstances can be used ...

查看更多
女痞
3楼-- · 2020-05-02 13:37

From Android documentation:

When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

To fix second problem you must restore data form some other source (service that plays music, restore info from application context or singleton, etc.)

For more info check this.

查看更多
登录 后发表回答