innershows
innershows

Reputation: 64

How to avoid startActivty(new Intent()) method starting more than one Activities?

I just do my project but when I test my app I found that then I touch the screen by using more than one fingers my app may start two or three different activities. The activities all go to the back stack. Is this a bug in Android framework? But I can't reappear this condition, it just happened.

So, have you guys ever have this problem? Please come and discuss with me. If you do; Thanks.

Supply:

And here is My xml file , when I click the different RelativeLayout at the same time , it happened.

I tried this afternoon , but this condition is not appear anymore. Now I am confusing.

    <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>

and this is my java code .

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


}

Upvotes: 1

Views: 81

Answers (3)

Akash Jindal
Akash Jindal

Reputation: 505

You can use this approach.......

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

Happy coding

Upvotes: 0

mach
mach

Reputation: 8395

You need to define the launch mode. There are at least two ways of solving this by either using the manifest file (hint: singleTop) or by using Intent flags (hint: FLAG_ACTIVITY_SINGLE_TOP).

Good luck!

Upvotes: 1

localhost
localhost

Reputation: 5598

Simpliest approach would be disable control that starts your Activity (button for example) after 1 click, and enable it later with some condition or action. Try it.

Upvotes: 0

Related Questions