gpt4 book ai didi

java - 从卡片滚动器启动单个 Activity 的正确方法

转载 作者:行者123 更新时间:2023-12-01 12:15:33 26 4
gpt4 key购买 nike

我创建了一个玻璃应用程序,其中有使用 CardScroller 的 CardScrollActivity 并从中导航到其他 Activity ,如下所示:

public class CardScrollActivity extends Activity {

private List<CardBuilder> mCards;
private CardScrollView mCardScrollView;
private ExampleCardScrollAdapter mAdapter;

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


mCardScrollView = new CardScrollView(this);
mAdapter = new ExampleCardScrollAdapter();
mCardScrollView.setAdapter(mAdapter);
mCardScrollView.activate();

setupClickListener();


setContentView(mCardScrollView);
}

private void setupClickListener() {
// TODO Auto-generated method stub
mCardScrollView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

log(" onitemclick called " );

if(id == 0)
{
log(" onitemclick called and select 1 item " );

Intent myIntent = new Intent(getBaseContext(),
Main2Activity.class);
startActivity(myIntent);
}

if(id == 1)
{
log(" onitemclick called and select 2 item " );
Intent myIntent = new Intent(getBaseContext(),
CompassActivity.class);
startActivity(myIntent);
}

if(id == 2)
{
log(" onitemclick called and select 3 item " );
Intent myIntent = new Intent(getBaseContext(),
GpsActivity.class);
startActivity(myIntent);
}
}
});

}

现在,问题是,当我从 Cardscroller 中选择任何项目时,堆栈跟踪显示启动 Activity 多次而不是一次,为什么它选择多次,请帮忙?

最佳答案

老实说,如果您不提供 LogCat 错误,我无能为力。

我唯一能说的是,也许您可​​以尝试完成当前的 Activity ,以确保在启动新 Activity 后它不会做任何奇怪的事情。为此,您需要将标志 FLAG_ACTIVITY_CLEAR_TOP 添加到您的 Intent,然后在启动 Intent 后立即调用 finish()

对于一个 Intent 调用,它看起来像这样:

Intent intent = new Intent(this, NewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

这是适合您的实现:

if(id == 0) {
log(" onitemclick called and select 1 item " );
Intent intent = new Intent(getBaseContext(), Main2Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} else if(id == 1) {
log(" onitemclick called and select 2 item " );
Intent intent = new Intent(getBaseContext(), CompassActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} else if(id == 2) {
log(" onitemclick called and select 3 item " );
Intent intent = new Intent(getBaseContext(), GpsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}

关于java - 从卡片滚动器启动单个 Activity 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com