Android: How NOT to save instance state?

不问归期 提交于 2019-12-11 07:41:28

问题


My app has login, once the access is granted, it creates a new html file to the sdcard, the output or the appearance of that html file depends on the account of the logger.

After writing, the app goes to the next activity

        Intent nextActivity = new Intent(MyMainAct.this, AppView.class);
        startActivity(nextActivity);

the AppView class is the next activity in which the UI is a webview. It views the html created on login.

When I click "login as different user" button, I go back to the Main Activity which is MyMainAct. I use the following code:

        Intent nextActivity = new Intent(AppView.this, MyMainAct.class);
        startActivity(nextActivity);

When I logged in again (as different user), I expected a different UI since the html created is different. But it did not happen. The same html of the first logger is presented by the webview.

I looked into the html code created by the second logger, it is different from the first. but the webview presents the html of the first logger.

Is it about instance state? That's what I think, that's why I don't want to save instance state (bundle).

EDIT:

How do I ignore savedInstanceState of my App?

my MainActivity:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

my AppView Activity:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

回答1:


Make sure that when user chooses the "login as different user" option, activity showing the HTML is finished (i.e. call the finish method on it). If you then ignore the savedInstanceState parameter in its onCreate event (which you likely do, I presume), the next time this activity is started it will be totally new instance of it.




回答2:


Try this and see if it works.

Intent nextActivity = new Intent(AppView.this, MyMainAct.class);
startActivity(nextActivity);
finish();


来源:https://stackoverflow.com/questions/6197594/android-how-not-to-save-instance-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!