从卡片滚动器启动单个活动的正确方法

时间:2014-11-20 06:58:17

标签: java android google-glass google-gdk

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

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中选择任何项目时,堆栈跟踪显示多次启动活动而不是一次,为什么它会多次选择plz帮助?

1 个答案:

答案 0 :(得分:1)

老实说,如果没有您提供LogCat错误,我无法提供帮助。

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

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

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(); 
}