什么 Android 事件只调用一次直到 Activity 被销毁?

发布于 2024-12-29 00:40:47 字数 201 浏览 1 评论 0原文

我正在寻找一个答案(但我可能会问错误的问题)

问题-任何事件是否只被调用一次 TOTAL 直到活动被销毁?

我问这个问题是因为当我的用户将手机旋转到横向时, oncreate 和 onstart 都会被调用,从而导致某种重新加载。

我正在寻找一个可以将行为放入其中的事件,该事件只会运行 1 次(直到该活动被终止)

提前谢谢

I'm looking for a single answer (but I might be asking the wrong question)

Question- does any event only get called once TOTAL until an activity is destroyed?

I ask because when my user rotates the phone to landscape oncreate and onstart are both invoked causing a reload of sorts.

I'm looking for an event that I could put behavior into that would only get run 1x (until the activity is killed)

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫负雪 2025-01-05 00:40:47

如果它特定于 Activity,只需检查 onCreate 事件中的 savingInstanceState 参数。如果为空,则运行您的代码,如果不是,则您的代码已经运行。

示例:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if(savedInstanceState == null) {
        // Run your code
    }        
}

第一次运行 onCreate 时,savedInstanceState 将始终为 null,之后将被填充。

If it is specific to the Activity just check your savedInstanceState parameter in the onCreate event. If it is null, run your code, if not, your code has already been run.

Example:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if(savedInstanceState == null) {
        // Run your code
    }        
}

savedInstanceState will always be null when onCreate is run for the first time, and it will be populated thereafter.

不醒的梦 2025-01-05 00:40:47

您并没有真正指定您想要用它做什么,所以我不能保证这适合您的使用,但是 Application.onCreate 仅调用一次。

You don't really specify what you're trying to do with it, so I can't guarantee this is appropriate for your use, but Application.onCreate is only called once.

秋凉 2025-01-05 00:40:47

如果您想消除在方向更改时重新创建活动,您可以侦听清单中的配置更改。

    <activity
            android:name=".MyActivity"
            android:configChanges="orientation" >
    </activity>

然后您可以像这样覆盖 onConfigurationChanged:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged( newConfig );

    LinearLayout main = (LinearLayout) findViewById( R.id.mainLayout );
    main.requestLayout();
}

重新创建布局,使其与新方向匹配,而无需重新创建整个活动。

If you want to eliminate the recreation of your activity on an orientationchange you can listen for configchanges in the manifest.

    <activity
            android:name=".MyActivity"
            android:configChanges="orientation" >
    </activity>

And then you can override onConfigurationChanged like so:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged( newConfig );

    LinearLayout main = (LinearLayout) findViewById( R.id.mainLayout );
    main.requestLayout();
}

to recreate the layout so that it matches the new orientation, without recreating the entire activity.

溺渁∝ 2025-01-05 00:40:47

检查 http://developer.android.com/guide/topics/resources/ runtime-changes.html 处理配置更改并维护它们之间的大量数据...如果您需要在配置更改之间维护的只是设置,您可以使用 onSavedInstanceState() 和 onRestoreInstanceState()回调和给定的捆绑包。

Check http://developer.android.com/guide/topics/resources/runtime-changes.html to handle configuration changes and to maintain your huge data between them...if all you need to maintain between the configuration change is just the settings,you can use the onSavedInstanceState() and onRestoreInstanceState() callbacks and the given bundles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文