Android: How NOT to save instance state?

2019-07-24 23:23发布

问题:

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