如何防止 activity 使用以前的 InstanceState 数据重新启动
How to prevent activity re-starting with previous InstanceState data
我有一个从主 activity 打开的 activity。当它使用后退按钮关闭然后重新启动时,它使用以前的实例状态打开而不是像新的一样打开。
主要activity
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();
}
我确定我缺少的是简单的东西。
总结:
我希望应用程序在屏幕方向发生变化时表现良好,但是当用户按下后退按钮时,我希望之前的状态消失。
在 PerformActivity.This 中添加此代码将在按下后退按钮时完成 activity。
@Override
public void onBackPressed() {
if(null!=this){
finish();
}
super.onBackPressed();
}
我有一个从主 activity 打开的 activity。当它使用后退按钮关闭然后重新启动时,它使用以前的实例状态打开而不是像新的一样打开。
主要activity
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();
}
我确定我缺少的是简单的东西。
总结: 我希望应用程序在屏幕方向发生变化时表现良好,但是当用户按下后退按钮时,我希望之前的状态消失。
在 PerformActivity.This 中添加此代码将在按下后退按钮时完成 activity。
@Override
public void onBackPressed() {
if(null!=this){
finish();
}
super.onBackPressed();
}