gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:27:49 25 4
gpt4 key购买 nike

我只是在做我的项目,但是当我测试我的应用程序时,我发现如果我使用多个手指触摸屏幕,我的应用程序可能会启动两个或三个不同的 Activity 。所有 Activity 都进入后台堆栈。这是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);
}
}


}

最佳答案

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

祝你好运!

关于android - 如何避免 startActivity(new Intent()) 方法启动多个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701381/

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