Android12withActivityRecreateFunction
创始人
2024-09-30 21:30:35
0

Android 12新增了Activity重建功能,可以让应用程序在系统恢复资源时更快地重建Activity。此功能基于API 31。

使用此功能需要在manifest文件中声明以下权限:


此外,在Activity中,可以将以下属性添加到manifest文件中的 标签中:

android:alwaysRetainTaskState="true"

这将使Activity保留其任务状态,即使销毁和重建都可以保持。

为了使Activity更快地重建,可以在Activity中覆盖onSaveInstanceState()方法,并在其中保留应用程序状态。然后,在onCreate()方法中恢复这些状态。

示例代码:

public class MainActivity extends AppCompatActivity {
    private int mCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState != null) {
            mCounter = savedInstanceState.getInt("counter");
        }

        TextView counterTextView = findViewById(R.id.counter_text_view);
        counterTextView.setText("Counter: " + mCounter);

        Button incrementButton = findViewById(R.id.increment_button);
        incrementButton.setOnClickListener(v -> {
            mCounter++;
            counterTextView.setText("Counter: " + mCounter);
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("counter", mCounter);
    }
}

相关内容

热门资讯

透视举措!poker worl... 透视举措!poker world辅助(透视)xpoker辅助怎么用(辅助)确实一直总是有神器(哔哩哔...
透视课程!newpoker怎么... 透视课程!newpoker怎么安装脚本(WPK有透视)确实是真的有辅助神器(哔哩哔哩)1、下载好ne...
透视课程!sohoo辅助(透视... 透视课程!sohoo辅助(透视)epoker免费透视脚本(辅助)一贯有软件(哔哩哔哩)1、epoke...
透视方案!xpoker辅助神器... 透视方案!xpoker辅助神器(透视)悦扑克脚本(辅助)总是真的有神器(哔哩哔哩)1、上手简单,内置...
透视要领!悦扑克脚本(透视)p... 透视要领!悦扑克脚本(透视)pokemmo免费脚本(辅助)原来真的有攻略(哔哩哔哩)所有人都在同一条...
透视总结!约局吧透视挂下载(W... 透视总结!约局吧透视挂下载(WePoKer亲友圈)真是是真的有辅助脚本(哔哩哔哩)1、用户打开应用后...
透视机巧!hhpoker有后台... 透视机巧!hhpoker有后台操作吗(WePoKer线上)总是真的是有辅助神器(哔哩哔哩)hhpok...
透视积累!红龙poker辅助(... 透视积累!红龙poker辅助(透视)哈糖大菠萝攻略(辅助)真是有神器(哔哩哔哩)1、哈糖大菠萝攻略辅...
透视烘培!拱趴大菠萝挂哪里(W... 透视烘培!拱趴大菠萝挂哪里(WePoKer插件)其实存在有辅助教程(哔哩哔哩)透视烘培!拱趴大菠萝挂...
透视窍要!智星菠萝辅助(透视)... 透视窍要!智星菠萝辅助(透视)约局吧怎么看有没有挂(辅助)真是有软件(哔哩哔哩)1、不需要AI权限,...