简体   繁体   English

在Activity中重新创建后未调用onResume

[英]onResume not called after recreate in Activity

I'm calling recreate in onActivityResult of MainActivity when certain changes are made in the app settings. 当在应用程序设置中进行某些更改时,我正在调用MainActivity onActivityResultrecreate After recreation, onResume is not called. 娱乐后,不调用onResume

I'm also getting the error: 我也得到了错误:

E/ActivityThread: Performing pause of activity that is not resumed

From this question, I understood that this function can't be called from onResume . 这个问题,我明白无法从onResume调用此函数。 But I'm calling them from onActivityResult . 但是我从onActivityResult调用它们。 Also using handler to call recreate resolves the problem, but causes a blink that looks bad to the user. 同时使用handler来调用recreate可以解决问题,但会导致对用户看起来不好的闪烁。 What could be the possibly wrong here? 这可能是什么问题? How can I use recreate without a Handler ? 如何recreate没有Handler情况下使用recreate

Any ideas will be appreciated. 任何想法将不胜感激。 Thanks! 谢谢!

OnActivityResult() is called before onResume(). 在onResume()之前调用OnActivityResult()。 What you can do is set a flag in the OnActivityResult() that you can check in the onResume() and if the flag is true you can recreate the activity. 你可以做的是在OnActivityResult()中设置一个标志,你可以在onResume()中检查,如果标志为true,你可以重新创建活动。

What you could actually do is to finish the activity and start the same one, isntead of recreating it. 您实际可以做的是完成活动并启动相同的活动,而不是重新创建活动。 You will get the same effect. 你会得到同样的效果。 it could be something like this: 它可能是这样的:

public class MainActivity extends AppCompatActivity   {

private boolean shouldRecreate = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("AG", "onCreate() called");
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivityForResult(intent, 0);
}

@Override
protected void onResume() {
    super.onResume();
    if (shouldRecreate){
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 0){
        shouldRecreate = true;
    }
}
}

我终于解决了这个问题,方法是将SettingsActivity中的BroadcastReciever发送到MainActivityBroadcastReciever ,并在onRecieve()调用recreate() onRecieve()

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

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