如何避免startActivity(new Intent())方法启动多个活动?

时间:2014-12-30 07:36:14

标签: android

我只是做我的项目,但是当我测试我的应用程序时,我发现我通过多个手指触摸屏幕,我的应用程序可能会启动两到三个不同的活动。 所有活动都进入后台。这是Android框架中的错误吗?但我不能再出现这种情况,它刚刚发生。

那么,你们有没有这个问题?请过来和我讨论。如果你这样做;感谢。

供应:

这是我的xml文件,当我同时点击不同的RelativeLayout时,它发生了。

我今天下午试过,但这种情况不再出现了。现在我很困惑。

    <LinearLayout android:layout_width="match_parent"
                  android:orientation="vertical"
                  android:background="@color/appDefaultSingleBlockBackground"
                  android:layout_height="wrap_content">
        <RelativeLayout android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:clickable="true"
                        android:background="@drawable/mine_bg"
                        android:id="@+id/mine_goto_personal_info_btn"
                >

            <TextView android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:layout_centerVertical="true"
                      android:gravity="center_vertical"
                      android:id="@+id/mine_nick_and_avatar"
                      android:drawablePadding="10dp"
                      android:layout_marginLeft="15dp"
                      style="@style/TextTitle"
                      android:textColor="@color/appDefaultSingleBlockBackground"
                      android:text=""/>

            <ImageView android:layout_width="wrap_content"
                       android:layout_height="match_parent"
                       android:src="@drawable/venue_maxbutton"
                       android:layout_centerVertical="true"
                       android:layout_alignParentRight="true"
                       android:layout_marginRight="15dp"
                    />

        </RelativeLayout>


        <RelativeLayout android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="@color/appDefaultSingleBlockBackground"
                        android:clickable="true"
                        android:id="@+id/mine_goto_interesting_venue"
                >

            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerVertical="true"
                      android:gravity="center_vertical"
                      android:drawablePadding="10dp"
                      android:layout_marginLeft="15dp"
                      style="@style/TextNomal"
                      android:drawableLeft="@drawable/information_attentionbutton"
                      android:text="@string/myAtentionVenue"/>

            <ImageView android:layout_width="wrap_content"
                       android:layout_height="match_parent"
                       android:src="@drawable/venue_maxbutton"
                       android:layout_centerVertical="true"
                       android:layout_alignParentRight="true"
                       android:layout_marginRight="15dp"
                    />

        </RelativeLayout>


        <View android:layout_width="match_parent"
              android:layout_height="1dp"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:background="@color/dividerdefault"
                />

        <RelativeLayout android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="@color/appDefaultSingleBlockBackground"
                        android:clickable="true"
                        android:id="@+id/mine_goto_setting"
                >

            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerVertical="true"
                      android:gravity="center_vertical"
                      android:drawablePadding="10dp"
                      android:layout_marginLeft="15dp"
                      style="@style/TextNomal"
                      android:drawableLeft="@drawable/information_setbutton"
                      android:text="@string/setting"/>

            <ImageView android:layout_width="wrap_content"
                       android:layout_height="match_parent"
                       android:src="@drawable/venue_maxbutton"
                       android:layout_centerVertical="true"
                       android:layout_alignParentRight="true"
                       android:layout_marginRight="15dp"
                    />

        </RelativeLayout>
        <View android:layout_width="match_parent"
              android:layout_height="1dp"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:background="@color/dividerdefault"
                />

        <RelativeLayout android:layout_width="match_parent"
                        android:layout_height="50dp"

                        android:background="@color/appDefaultSingleBlockBackground"
                        android:clickable="true"
                        android:id="@+id/mine_goto_youhuijuan"
                >

            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerVertical="true"
                      android:gravity="center_vertical"
                      android:drawablePadding="10dp"
                      android:layout_marginLeft="15dp"
                      style="@style/TextNomal"
                      android:drawableLeft="@drawable/information_discountbutton"
                      android:text="@string/youhuijuan"/>

            <ImageView android:layout_width="wrap_content"
                       android:layout_height="match_parent"
                       android:src="@drawable/venue_maxbutton"
                       android:layout_centerVertical="true"
                       android:layout_alignParentRight="true"
                       android:layout_marginRight="15dp"
                    />

        </RelativeLayout>

        <View android:layout_width="match_parent"
              android:layout_height="1dp"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:background="@color/dividerdefault"
                />

        <RelativeLayout android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="@color/appDefaultSingleBlockBackground"
                        android:clickable="true"
                        android:id="@+id/mine_goto_my_rest_money"
                >

            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerVertical="true"
                      android:gravity="center_vertical"
                      android:drawablePadding="10dp"
                      android:layout_marginLeft="15dp"
                      style="@style/TextNomal"
                      android:drawableLeft="@drawable/information_balancebutton"
                      android:text="@string/myRest"/>

            <ImageView android:layout_width="wrap_content"
                       android:layout_height="match_parent"
                       android:src="@drawable/venue_maxbutton"
                       android:layout_centerVertical="true"
                       android:layout_alignParentRight="true"
                       android:layout_marginRight="15dp"
                    />

        </RelativeLayout>


        <View android:layout_width="match_parent"
              android:layout_height="1dp"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:background="@color/dividerdefault"
                />

        <RelativeLayout android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="@color/appDefaultSingleBlockBackground"
                        android:clickable="true"
                        android:id="@+id/mine_goto_secure"
                >

            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerVertical="true"
                      android:gravity="center_vertical"
                      android:drawablePadding="10dp"
                      android:layout_marginLeft="15dp"
                      style="@style/TextNomal"
                      android:drawableLeft="@drawable/information_accountssafebutton"
                      android:text="@string/secure"/>

            <ImageView android:layout_width="wrap_content"
                       android:layout_height="match_parent"
                       android:src="@drawable/venue_maxbutton"
                       android:layout_centerVertical="true"
                       android:layout_alignParentRight="true"
                       android:layout_marginRight="15dp"
                    />

        </RelativeLayout>

        <View android:layout_width="match_parent"
              android:layout_height="1dp"
              android:background="@color/dividerdefault"
                />

    </LinearLayout>

这是我的java代码。

private void init(View ret) {
    final TextView txtAvatar = (TextView) ret.findViewById(R.id.mine_nick_and_avatar);

    final RelativeLayout gotoPersonal = (RelativeLayout) ret.findViewById(R.id.mine_goto_personal_info_btn);
    RelativeLayout gotoInterest = (RelativeLayout) ret.findViewById(R.id.mine_goto_interesting_venue);
    RelativeLayout gotoSetting = (RelativeLayout) ret.findViewById(R.id.mine_goto_setting);
    RelativeLayout gotoYouhuijuan = (RelativeLayout) ret.findViewById(R.id.mine_goto_youhuijuan);
    RelativeLayout gotoMyRestMoney = (RelativeLayout) ret.findViewById(R.id.mine_goto_my_rest_money);
    RelativeLayout gotoSecure = (RelativeLayout) ret.findViewById(R.id.mine_goto_secure);

    gotoPersonal.setTag("gotoPersonal");
    gotoInterest.setTag("gotoInterest");
    gotoSetting.setTag("gotoSetting");
    gotoYouhuijuan.setTag("gotoYouhuijuan");
    gotoMyRestMoney.setTag("gotoMyRestMoney");
    gotoSecure.setTag("gotoSecure");


    try {
        Object o = SharedPreferenceUtils.readInfo(mActivity, ConstData.UserInfo[1]);
        if (o != null && !"null".equals(o)) {
            txtAvatar.setText((String) o);
        } else {
            o = SharedPreferenceUtils.readInfo(mActivity, ConstData.UserInfo[10]);
            if (o != null && !"null".equals(o))
                txtAvatar.setText("KD" + o);
        }
    } catch (Exception e) {

    }


    Object o1 = SharedPreferenceUtils.readInfo(mActivity, ConstData.UserInfo[2]);


    if (o1 != null)
        MyApplication.downloader.download("http://" + o1, new ImageDownloadStateListener() {
            @Override
            public void loading() {
            }

            @Override
            public void loadSuccess(Bitmap bitmap, String url) {
                try {
                    bitmap = Tools.transforCircleBitmap(bitmap);

                    BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);

                    gotoPersonal.measure(0, 0);
                    int measuredHeight = gotoPersonal.getMeasuredHeight();

                    LogHelper.print("==height" + measuredHeight);

                    drawable.setBounds(0, 0, measuredHeight / 5 * 4, measuredHeight / 5 * 4);

                    txtAvatar.setCompoundDrawables(drawable, null, null, null);
                } catch (Exception e) {
                    //no nothing
                }
            }

            @Override
            public void loadFailed() {

            }
        });


    gotoPersonal.setOnClickListener(this);
    gotoInterest.setOnClickListener(this);
    gotoSetting.setOnClickListener(this);
    gotoYouhuijuan.setOnClickListener(this);
    gotoMyRestMoney.setOnClickListener(this);
    gotoSecure.setOnClickListener(this);
}

@Override
public void onResume() {

    initActionBar();

    super.onResume();

    MobclickAgent.onPageStart(getClassName()); //统计页面
}

@Override
public void onPause() {
    super.onPause();

    MobclickAgent.onPageEnd(getClassName());
}

private String getClassName() {
    String canonicalName = this.getClass().getCanonicalName();
    String[] split = canonicalName.split("\\.");
    return split[split.length - 1];
}

private void initActionBar() {
    MyActivity activity = (MyActivity) mActivity;

    activity.setActionBarLeftImg(new ColorDrawable(Color.TRANSPARENT), false);
    activity.setActionBarRightImg(new ColorDrawable(Color.TRANSPARENT));
    activity.setOnActionBarLeftClickListener(null);
    activity.setOnActionBarRightClickListener(null);
    activity.setActionBarTitle("个人中心");
}



@Override
public void onClick(View v) {

    Object tag = v.getTag();
    if (tag != null) {
        String str = (String) tag;

        if (mActivity == null) {
            return;
        }


        if (!((MyActivity) (mActivity)).isLogin) {
            Intent intent = new Intent(mActivity, LoginActivity.class);
            startActivity(intent);
            return;
        }

        if (!TextUtils.isEmpty(str))
            if ("gotoPersonal".equals(str)) {

                Intent intent = new Intent(mActivity, PersonalInfoActivity.class);
                startActivity(intent);
            } else if ("gotoInterest".equals(str)) {

                Intent intent = new Intent(mActivity, VenueListActivity.class);
                intent.putExtra("isInterests", true);

                startActivity(intent);
            } else if ("gotoSetting".equals(str)) {


                Intent intent = new Intent(mActivity, SettingActivity.class);
                startActivity(intent);
            } else if ("gotoYouhuijuan".equals(str)) {


                Intent intent = new Intent(mActivity, FavorableActivity.class);
                startActivity(intent);

            } else if ("gotoSecure".equals(str)) {


                Intent intent = new Intent(mActivity, SecureActivity.class);
                startActivity(intent);

            } else if ("gotoMyRestMoney".equals(str)) {


                Intent intent = new Intent(mActivity, MyRestActivity.class);
                startActivity(intent);
            }
    }


}

3 个答案:

答案 0 :(得分:1)

您需要定义启动模式。至少有两种方法可以通过using the manifest file(提示:singleTop)或使用Intent标志(提示:FLAG_ACTIVITY_SINGLE_TOP)来解决这个问题。

祝你好运!

答案 1 :(得分:0)

最简单的方法是禁用控制,在单击后启动您的Activity(例如按钮),稍后在某些条件或操作时启用它。试试吧。

答案 2 :(得分:0)

你可以使用这种方法.......

((Button)findViewById(R.id.someButton)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ((Button)findViewById(R.id.someButton)).setEnabled(false);
            }
        });

快乐编码