在启动新的intent后减慢活动加载

oyjwcjzk  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(205)

在主活动(mainactivity.java)中,有几个图像充当按钮。按下时,会触发一个事件隐藏ui(但保持主要活动可见),然后创建并启动新的意图。问题是新意图/活动的加载似乎很慢(0.5到3秒),特别是在通过android studio在手机上重新安装应用程序(xperia 10 ii)进行调试之后。定时的变化也取决于按下哪个按钮。因为每个按钮都会带来不同的活动,而且“较重”的活动需要更长的时间,所以我认为问题与活动中加载了多少有关 onCreate() 方法。
我的猜测是,我需要在不同的线程中分离从内存加载的内容和为显示而创建的内容。如果是这样的话,我不知道怎么做。
我很快读完了这篇文章,但它没有说明如何做到它所说的:通过线程提高性能
我还发现了这篇文章,因为它的名字,我认为这就是我要找的。但答案似乎与标题中描述的问题不符:启动新活动的意图非常缓慢:(
我还尝试按照本文的建议删除xml文件中的权重,但没有成功:活动加载缓慢
mainactivity.java

public class MainActivity extends AppCompatActivity {

    // Used to load and save data (e.g.: locked and unlocked in-game items)
    public static DataManager dataManager;

    // Instance of player (e.g.: gear and inventory)
    public static Player player;

    private ActivityResultLauncher <Intent> exampleActivityLauncher;
    private ImageButton buttonExample;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dataManager = new DataManager(this);

        player = new Player();

        this.exampleActivityLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            uiVisible(View.VISIBLE);
        });

        this.buttonExample = this.findViewById(R.id.example);

        this.initExample();
    }

    @Override
    protected void onResume () {
        super.onResume();
        this.initExample();
    }

    private void initExample () {
        this.buttonExample.setOnClickListener(view -> {
            uiVisible(View.INVISIBLE);
            Intent intent = new Intent(MainActivity.this, ExampleActivity.class);
            exampleActivityLauncher.launch(intent);
        });
    }

    private void uiVisible (int visibility) {
        this.buttonExample.setVisibility(visibility);
    }
}

exampleactivity.java

public class ExampleActivity extends AppCompatActivity {

    // Exetnds "ArrayAdapter <NamedImageView>"
    private static CategoryAdapter adapterCategory;
    // Extends "ArrayAdapter <ExampleItem>"
    private static ExampleAdapter adapterItems;

    // Two-way grid with additional method "getSelection(int position)"
    // Enable selection between listA, listB and listC
    private static SelectionTwoWayGridView selectorCategory;
    // Enable item selection in chosen list (i.e.: category)
    private static SelectionTwoWayGridView selectorItem;

    private static List <CustomItem> listA;
    private static List <CustomItem> listB;
    private static List <CustomItem> listC;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.initListA();
        this.initListB();
        this.initListC();

        this.setContentView(R.layout.example_activity);

        adapterItems = new ExampleAdapter(this, R.layout.example_item);
        adapterItems.addAll(listA);
        adapterItems.setList(listA);
        selectorItem = this.findViewById(R.id.selector_item);
        selectorItem.setAdapter(adapterItems);
        selectorItem.setSelection(MainActivity.dataManager.loadA());

        adapterCategory = new CategoryAdapter(this, R.layout.example_item, listCategory);
        selectorCategory = this.findViewById(R.id.selector_category);
        selectorCategory.setAdapter(adapterCategory);

        selectorCategory.setOnItemClickListener((parent, view, position, id) -> {
            currentCategory = listCategory.get(position).getName();
            adapterItems.clear();
            switch (currentCategory) {
                case CATEGORY_A :
                    adapterItems.addAll(listA);
                    adapterItems.setList(listA);
                    selectorItem.setSelection(MainActivity.dataManager.loadA());
                    break;
                case CATEGORY_B :
                    adapterItems.addAll(listB);
                    adapterItems.setList(listB);
                    selectorItem.setSelection(MainActivity.dataManager.loadB());
                    break;
                case CATEGORY_C :
                    adapterItems.addAll(listC);
                    adapterItems.setList(listC);
                    selectorItem.setSelection(MainActivity.dataManager.loadC());
                    break;
            }
            selectorCategory.setSelection(position);
            adapterCategory.notifyDataSetChanged();
            selectorItem.invalidateViews();
        });

        selectorItem.setOnItemClickListener((parent, view, position, id) -> {
            ExampleItem item = ((ExampleItem) parent.getItemAtPosition(position));
            if (! item.isUnlocked()) {
                if (MainActivity.player.getCurrentPoints() >= item.getUnlockAmount()) {
                    Intent intent = new Intent(ExampleActivity.this, ActivityPopUpQuestion.class);
                    intent.putExtra("FROM", NAME);
                    intent.putExtra("position", position);
                    intent.putExtra("image", item.getImage());
                    intent.putExtra("title", "");
                    intent.putExtra("type", ActivityPopUpQuestion.TYPE_UNLOCK);
                    startActivity(intent);
                    // User can choose to unlock or to cancel
                    // Unlock uses the below method "unlockItem(position)"
                }
                else {
                    Toast.makeText(ExampleActivity.this, R.string.toast_not_enough_points_to_unlock, Toast.LENGTH_SHORT).show();
                }
            }
            else {
                switch (currentCategory) {
                    case CATEGORY_A :
                        MainActivity.player.A = position;
                        break;
                    case CATEGORY_B :
                        MainActivity.player.B = position;
                        break;
                    case CATEGORY_C :
                        MainActivity.player.C = position;
                        break;
                }
                selectorItem.setSelection(position);
                adapterItems.notifyDataSetChanged();
                MainActivity.dataManager.saveProgress();
            }
        });
    }

    private void initListA () {
        listA = new ArrayList <> ();
        CustomItem startItem = new CustomItem(this, 0, R.drawable.icon_A_0);
        startItem.unlock();
        listA.add(startItem);
        listA.add(new CustomItem(this, 50, R.drawable.icon_A_1));
        listA.add(new CustomItem(this, 50, R.drawable.icon_A_2));
        listA.add(new CustomItem(this, 50, R.drawable.icon_A_3));
        listA.add(new CustomItem(this, 250, R.drawable.icon_A_4));
        listA.add(new CustomItem(this, 250, R.drawable.icon_A_5));
        listA.add(new CustomItem(this, 250, R.drawable.icon_A_6));
        listA.add(new CustomItem(this, 250, R.drawable.icon_A_7));
        listA.add(new CustomItem(this, 500, R.drawable.icon_A_8));
        listA.add(new CustomItem(this, 500, R.drawable.icon_A_9));
        listA.add(new CustomItem(this, 1000, R.drawable.icon_A_10));
        this.updateListWithUnlocks(listA, MainActivity.dataManager.loadUnlocksA());
    }

    private void initListB () {
        listB = new ArrayList <> ();
        CustomItem startItem = new CustomItem(this, 0, R.drawable.icon_B_0);
        startItem.unlock();
        listB.add(startItem);
        listB.add(new CustomItem(this, 50, R.drawable.icon_B_1));
        listB.add(new CustomItem(this, 50, R.drawable.icon_B_2));
        listB.add(new CustomItem(this, 50, R.drawable.icon_B_3));
        listB.add(new CustomItem(this, 250, R.drawable.icon_B_4));
        listB.add(new CustomItem(this, 250, R.drawable.icon_B_5));
        listB.add(new CustomItem(this, 500, R.drawable.icon_B_6));
        this.updateListWithUnlocks(listB, MainActivity.dataManager.loadUnlocksB());
    }

    private void initListC () {
        listC = new ArrayList <> ();
        CustomItem startItem = new CustomItem(this, 0, R.drawable.icon_C_0);
        startItem.unlock();
        listC.add(startItem);
        listC.add(new CustomItem(this, 50, R.drawable.icon_C_1));
        listC.add(new CustomItem(this, 50, R.drawable.icon_C_2));
        listC.add(new CustomItem(this, 50, R.drawable.icon_C_3));
        listC.add(new CustomItem(this, 50, R.drawable.icon_C_4));
        listC.add(new CustomItem(this, 50, R.drawable.icon_C_5));
        listC.add(new CustomItem(this, 250, R.drawable.icon_C_6));
        listC.add(new CustomItem(this, 250, R.drawable.icon_C_7));
        this.updateListWithUnlocks(listC, MainActivity.dataManager.loadUnlocksC());
    }

    public static void unlockItem (int position) {
        PersonalisationItem item = adapterItems.getList().get(position);
        MainActivity.player.removePoints(item.getUnlockAmount());
        item.unlock();
        selectorItem.invalidateViews();
        MainActivity.dataManager.saveUnlocks();
    }
}

使用上面的代码,看起来比较慢的是 ExampleActivity 按压后 buttonExample .
为什么在重新安装应用程序后,转换速度真的很慢?
为什么过渡很慢?
如何加快过渡?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题