简体   繁体   English

返回活动时恢复状态不起作用,savedInstanceState为null

[英]Restoring state not working when returning to activity, savedInstanceState is null

So im having some trouble restoring the state of my Activity . 所以我在恢复我的Activity状态时遇到了一些麻烦。 At this point I figure that its probably a problem with my understanding rather then anything else. 在这一点上,我认为这可能是我理解的问题,而不是其他任何问题。

My goal is to move from my MainActivity to a Main2Activity . 我的目标是从MainActivity转到Main2Activity Am I correct in thinking that when a user moves from one page to another, it should be done by changing Activity via Intent ? 我是否正确地认为,当用户从一页移动到另一页时,应该通过Intent更改Activity来完成? I am doing this like so: 我这样做是这样的:

The onCreate() for my MainActivity has this in it. 我的MainActivityonCreate()有这个。

Button currentButton = (Button) findViewById(R.id.button2);
currentButton.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.activity_main2);
            Intent nextIntent = new Intent(getApplicationContext(), Main2Activity.class);
            startActivity(nextIntent);
        }
    }
);

Which as I understand should call onCreate() , onStart() and onResume() for Main2Activity, then onSaveInstanceState() for MainActivity , then onStop() for MainActivity . 其中按我的理解应该叫onCreate() onStart()onResume()为Main2Activity,那么onSaveInstanceState()MainActivity ,那么onStop()MainActivity Ive overloaded all those functions with logging and seen that indeed they are being called and in that order. 我已经用日志记录重载了所有这些函数,并看到它们确实按照该顺序被调用。

Here is my onSaveInstanceState() for MainActivity : 这是我的MainActivity onSaveInstanceState()

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    System.out.println("onSaveInstanceState called, saving state");
    savedInstanceState.putInt("mySuperUniqueKey", testInt);

    super.onSaveInstanceState(savedInstanceState);
}

Once in Main2Activity , I return back to MainActivity in the same way. 进入Main2Activity ,我将以相同的方式返回MainActivity Ie findViewById() the button, overload its onClickListener() , create a new Intent and start it. 即按钮findViewById(),重载其onClickListener() ,创建一个新的Intent并启动它。

Then MainActivity class's onCreate() has this : 然后MainActivity类的onCreate()具有以下内容:

if (savedInstanceState != null) {
    System.out.println("savedInstanceState is not null");
    testInt = savedInstanceState.getInt("mySuperUniqueKey");
} else {
    System.out.println("savedInstanceState is null");
}

When returning back the MainActivity from Main2Activity , I can see from the logging that onCreate() , then onStart() , then onResume() for MainActivity is called, then onStop() for Main2Activity . Main2Activity返回MainActivity时,从日志中可以看到onCreate()then onStart()MainActivity onResume() onStop()Main2Activity onStop() Unfortunatly the logging shows that savedInstanceState always comes back as null. 不幸的是,该日志记录显示savedInstanceState始终返回为null。

To add to this, when in the emulator, switching the orientation back and forth causes this to work perfectly; 除此之外,当在仿真器中时,来回切换方向可以使其完美工作。 savedInstanceState is not null and features the saved testInt. savedInstanceState不为null,并且具有保存的testInt。 Thus I figure its a problem with my understanding and that there must be something im missing. 因此,根据我的理解,我认为这是一个问题,必须缺少一些东西。

My gradle has minSdkVersion set to 16, and targetSdkVersion set to 28. Am I maybe targeting too low a minSdkVersion? 我的gradle的minSdkVersion设置为16,targetSdkVersion设置为28。我可能将minSdkVersion设置的太低了吗?

I have read through the "Understand the Activity Lifecycle" on the official android developer documentation but still cant get it. 我已经阅读了官方android开发人员文档中的“了解活动生命周期”,但仍然无法获取。 https://developer.android.com/guide/components/activities/activity-lifecycle https://developer.android.com/guide/components/activities/activity-lifecycle

I did find similar problems but none of them match my situation exaclty, also the solutions they have suggested I am already doing anyway. 我确实发现了类似的问题,但都没有一个符合我的情况,还有他们建议我已经采取的解决方案。

Any insight would be greatly appreciated, thanks in advance. 在此先感谢您的任何见解。

The saved instance state bundle is intended to save the state of the current activity across things like orientation changes. 保存的实例状态包用于在方向更改之类的事物之间保存当前活动的状态。 It is not designed to persist across activities. 它并非旨在在各种活动中持续存在。 You should use Intent#putExtra : 您应该使用Intent#putExtra

nextIntent.putExtra("mySuperUniqueKey", testInt);

Then, in your next activity, access this passed value using: 然后,在您的下一个活动中,使用以下命令访问此传递的值:

int testInt = getIntent().getIntExtra("mySuperUniqueKey");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM