如何避免重新初始化的活动,需要较长的时间,用来初始化 [英] How Avoid re-initializing Activity That Takes a long time to initilize

查看:109
本文介绍了如何避免重新初始化的活动,需要较长的时间,用来初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个屏幕,而且它们之间进行切换按钮的应用程序。一个屏幕主要是一个ListView,但其他中有(AndEngine)游戏引擎,它可以需要一段时间的游戏引擎给init第一次创建活动。
所以它们之间的切换可以去一个新的游戏引擎时造成大的延误。不过,如果我只是用后退按钮时,previous游戏引擎加载速度更快。
有没有我可以指定只有一个混凝土的游戏引擎,并随时调出活动的该实例的方法吗?现在我用startIntent()来交换屏幕。是否有一些其他的方式?
的方式,让我只是有每个活动的它们之间的单一实例和交换?

I have an app with two screens, and buttons that switch between them. One screen is mainly a listView, but the other has a game engine in it (AndEngine) and it can take a while for the game engine to init the first time the activity is created. So switching between them can cause big delays when going to a new game Engine. However, if I just use the back button, the previous game engine loads much faster. Is there a way I can specify to crete only one game engine and always bring up that instance of the activity? Right now I am using startIntent() to swap screens. Is there some other way? A way that allows me to just have a single instance of each activity and swap between them?

推荐答案

Android提供了一个简单的方法来使用TabHost活动之间进行切换。你也可以用它来下 TabActivity 作为解释片段之间切换。或者,你可以一个的FrameLayout添加到您的活动,编程实例化片段,并附加/显示/在需要的时候隐藏起来。

Android provides a simple way to switch between activities using a TabHost. You can also use it to switch between Fragments as explained under TabActivity. Alternatively, you can add a FrameLayout to your activity, programmatically instantiate the fragments and attach/show/hide them when needed.

您RES /布局/ main.xml中是这样的:

Your res/layout/main.xml would look like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_height="fill_parent"
             android:layout_width="match_parent"
             android:id="@+id/mainframe">
</FrameLayout>

和假设V4支持库时,您的活动应该是这样的:

And assuming the v4 support library is used, your activity would look like this:

public MyActivity extends FragmentActivity {

    private Fragment mListFrag;
    private Fragment mGameFrag;

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

        mListFrag = new MyListFragment();
        mGameFrag = new MyGameFragment();

        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction()
            .add(
                R.id.mainframe,
                mListFrag,
                MyListFragment.class.getName())
            .add(
                R.id.mainframe,
                mGameFrag,
                MyGameFragment.class.getName())
            .detach(mGameFrag)
            .commit();

        fm.executePendingTransactions();
    }

    public void showList() {
        getSupportFragmentManager().beginTransaction()
            .hide(mGameFrag)
            .show(mListFrag)
            .commit();
    }

    public void showGame() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (mGameFrag.isDetached()) {
            ft.attach(mGameFrag);
        }
        ft.hide(mListFrag).show(mGameFrag).commit();
    }
}

观察到MyGameFragment.onCreateView不叫,直到它首先连接。在这之后,隐藏和显示的片段,用户可以毫不延迟切换。

Observe that MyGameFragment.onCreateView isn't called until it is first attached. After that, hiding and showing the fragments allows the user to switch without delay.

编辑:现在我明白你想用自己的按钮2活动。我已经更新了code,以反映这一点。从OnClickListeners只需调用相关的活动功能,像这样:

I realise now you wanted 2 activities with their own button. I've updated the code to reflect this. From the OnClickListeners simply call the relevant activity functions like so:

((MyActivity)MyListFragment.this.getActivity())showGame();

这篇关于如何避免重新初始化的活动,需要较长的时间,用来初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆