【问题标题】:Starting new activity from thread从线程开始新活动
【发布时间】:2015-06-02 00:37:18
【问题描述】:

我正在运行一个带有选项的 listView,当用户单击 listView 中的元素时,他们将重定向到一个新活动,当前活动将关闭。此代码运行但每次跳过大约 100 帧。我想知道是否有更有效的方法来执行此操作,以及我是否正确执行此操作。谢谢!

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    drawerLayout.closeDrawers();
    switch(position){
        case 0:
            // Sets listView element selected.
            selectListViewItemLeft(position);
            new Thread() {
                public void run() {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(Home_Page.this, Home_Page.class);
                                startActivity(intent);
                                finish();
                            }
                        });
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start();
            break;
        case 1:

            break;
    }
}

【问题讨论】:

  • runOnUiThread 不是必需的..

标签: android multithreading listview


【解决方案1】:

除非我误解了这个问题,否则您不需要在线程中启动新的活动,您可以启动一个新的活动:

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    drawerLayout.closeDrawers();
    switch(position){
        case 0:
            Intent intent = new Intent(Home_Page.this, Home_Page.class);
            startActivity(intent);
            finish();

            break;
        case 1:

            break;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多